架构-层-DAL:DAL
| ylbtech-架构-层-DAL:DAL |
DAL是数据访问层的英文缩写,即为数据访问层(Data Access Layer)。其功能主要是负责数据库的访问。简单地说就是实现对数据表的Select(查询)、Insert(插入)、Update(更新)、Delete(删除)等操作。
| 1.返回顶部 |
| 2.返回顶部 |
基本介绍

特点
设计
namespace MessageBoard.DataAccessLayer
{
public class Database
{
自定义方法()
{ }
}
}
using System.Data.SqlClient; public class DBConnection
{
SqlConnection conn = null; public DBConnection()
{
//TODO:创建对象
// Windows 身份验证
//conn = new SqlConnection("Server=.;Database=db1;Integrated Security=SSPI"); // 本地连接 // SQL Server 身份验证
// .,locahost,127.0.0.1 都代表本地
//conn.ConnectionString = "Server=.;Database=db1;Uid=sa;Pwd=123456";
//conn.ConnectionString = "Server=locahost;Database=db1;Uid=sa;Pwd=123456";
//conn.ConnectionString = "Server=127.0.0.1;Database=db1;Uid=sa;Pwd=123456";
conn = new SqlConnection( "Server=.;User ID=sa;Password=123456;Initial Catalog=db1;Min Pool Size=30;Max Pool Size=100;Connect Timeout=60" ); // 远程连接
} public SqlConnection Conn
{
get { return conn; }
set { conn = value; }
}
}
// 创建Conn对象
SqlConnection conn = new DBConnection().Conn;
| 3. 代码返回顶部 |
using System.Data.SqlClient; public class DBConnection
{
SqlConnection conn = null; public DBConnection()
{
//TODO:创建对象
// Windows 身份验证
//conn = new SqlConnection("Server=.;Database=db1;Integrated Security=SSPI"); // 本地连接 // SQL Server 身份验证
// .,locahost,127.0.0.1 都代表本地
//conn.ConnectionString = "Server=.;Database=db1;Uid=sa;Pwd=123456";
//conn.ConnectionString = "Server=locahost;Database=db1;Uid=sa;Pwd=123456";
//conn.ConnectionString = "Server=127.0.0.1;Database=db1;Uid=sa;Pwd=123456";
conn = new SqlConnection( "Server=.;User ID=sa;Password=123456;Initial Catalog=db1;Min Pool Size=30;Max Pool Size=100;Connect Timeout=60" ); // 远程连接
} public SqlConnection Conn
{
get { return conn; }
set { conn = value; }
}
}
public class ProductInfo
{
public int? ProductId { get; set; }
public string ProductName { get; set; }
public decimal? UnitPrice { get; set; }
public string Type { get; set; } }
using System.Collections.Generic; using System.Data.SqlClient; public class Product
{
/// <summary>
/// ylb: 1,GetAll
/// remark: 获取所有产品,并以productId降序排列
/// </summary>
/// <returns></returns>
public IList<ProductInfo> GetAll()
{ IList<ProductInfo> dals = new List<ProductInfo>(); string sql = "select productId,productName,unitPrice,type from Product"; SqlConnection conn = new DBConnection().Conn;
SqlCommand com = conn.CreateCommand(); com.CommandText = sql;
conn.Open();
try
{
SqlDataReader sdr = com.ExecuteReader();
while( sdr.Read() )
{
ProductInfo dal = new ProductInfo()
{
ProductId = sdr.GetInt32( ),
ProductName = sdr.GetString( ),
UnitPrice = sdr.GetDecimal( ),
Type = sdr.GetString( )
}; dals.Add( dal );
}
}
finally
{
conn.Close();
}
return dals;
} /// <summary>
/// ylb: 2,Add
/// remark: 添加一个产品
/// field: productName,unitPrice,type
/// </summary>
/// <param name="dal"></param>
public void Add( ProductInfo dal )
{ string sql = "insert into Product(productName,unitPrice,type) values(@productName,@unitPrice,@type)"; SqlConnection conn = new DBConnection().Conn;
SqlCommand com = conn.CreateCommand(); com.Parameters.Add( new SqlParameter( "@productName", dal.ProductName ) );
com.Parameters.Add( new SqlParameter( "@unitPrice", dal.UnitPrice ) );
com.Parameters.Add( new SqlParameter( "@type", dal.Type ) );
com.CommandText = sql; conn.Open();
try
{
com.ExecuteNonQuery();
}
finally
{
conn.Close();
} } /// <summary>
/// ylb: 3,GetModel
/// remark: 获得一个实体对象,根据productId
/// </summary>
/// <param name="productId"></param>
/// <returns></returns>
public ProductInfo GetModel( int productId )
{
ProductInfo dal = null; string sql = "select productId,productName,unitPrice,type from Product where productId=@productId"; SqlConnection conn = new DBConnection().Conn;
SqlCommand com = conn.CreateCommand(); com.Parameters.Add( new SqlParameter( "@productId", productId ) );
com.CommandText = sql;
conn.Open();
try
{
SqlDataReader sdr = com.ExecuteReader();
while( sdr.Read() )
{
dal = new ProductInfo()
{
ProductId = sdr.GetInt32( ),
ProductName = sdr.GetString( ),
UnitPrice = sdr.GetDecimal( ),
Type = sdr.GetString( )
};
}
}
finally
{
conn.Close();
}
return dal;
} /// <summary>
/// ylb: 4,Update
/// remark: 修改一条信息 ,根据productId
/// </summary>
/// <param name="dal"></param>
public void Update( ProductInfo dal )
{ string sql = "update Product set productName=@productName,unitPrice=@unitPrice,type=@type where productId=@productId"; SqlConnection conn = new DBConnection().Conn;
SqlCommand com = conn.CreateCommand(); com.Parameters.Add( new SqlParameter( "@productName", dal.ProductName ) );
com.Parameters.Add( new SqlParameter( "@unitPrice", dal.UnitPrice ) );
com.Parameters.Add( new SqlParameter( "@type", dal.Type ) );
com.Parameters.Add( new SqlParameter( "@productId", dal.ProductId ) );
com.CommandText = sql; conn.Open();
try
{
com.ExecuteNonQuery();
}
finally
{
conn.Close();
} } /// <summary>
/// ylb: 5,Delete
/// remark: 删除一条信息,根据productId
/// </summary>
/// <param name="productId"></param>
public void Delete( int productId )
{ string sql = "delete Product where productId=@productId"; SqlConnection conn = new DBConnection().Conn;
SqlCommand com = conn.CreateCommand(); com.Parameters.Add( new SqlParameter( "@productId", productId ) );
com.CommandText = sql; conn.Open();
try
{
com.ExecuteNonQuery();
}
finally
{
conn.Close();
}
}
}
| 4.返回顶部 |
| 5.返回顶部 |
| 6.返回顶部 |
![]() |
作者:ylbtech 出处:http://ylbtech.cnblogs.com/ 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。 |
架构-层-DAL:DAL的更多相关文章
- asp.net mvc 简单项目框架的搭建过程(一)对Bll层和Dal层进行充分解耦
学习asp.net 已经有近三个月的时间了,在asp.net mvc上花的时间最多,但个人真是有些菜,不得不说,asp.net mvc的水真的还是蛮深的.目前在公司实习,也见过公司几个项目的代码了.对 ...
- IOC的理解,整合AOP,解耦对Service层和Dal层的依赖
DIP依赖倒置原则:系统架构时,高层模块不应该依赖于低层模块,二者通过抽象来依赖依赖抽象,而不是细节 贯彻依赖倒置原则,左边能抽象,右边实例化的时候不能直接用抽象,所以需要借助一个第三方 高层本来是依 ...
- 三层架构BLL+DAL+Model & MVC & MVVM
三层架构 - 国内版 Binghttps://cn.bing.com/search?FORM=U227DF&PC=U227&q=%E4%B8%89%E5%B1%82%E6%9E%B6% ...
- 架构-层-Model:Model
ylbtech-架构-层-Model:Model 1.返回顶部 1. Model,意思是模特儿,模特儿是英文“model”的音译.模特一般来说要五官端正,身材良好,有气质,展示能力强,另外身高要具备一 ...
- 架构-层-BLL:BLL
ylbtech-架构-层-BLL:BLL 业务逻辑层(Business Logic Layer)无疑是系统架构中体现核心价值的部分.它的关注点主要集中在业务规则的制定.业务流程的实现等与业务需求有关的 ...
- 从架构层面谈web加载优化(个人整理)
最近听了阿里一位大牛的讲座,讲web架构优化对网页加载的影响,看完之后对他所讲的一些优化方法进行一些总结和整理,发现收获还是蛮多的,下面多为个人整理和个人见解,希望有说的不对的,能及时指出 1.DNS ...
- JDBC——架构层、驱动
JDBC(java Datebase Connector) jdbc驱动程序 四种类型: jdbc-odbc桥接驱动程序 Native-API JDBC-Net Native-Protocol (常见 ...
- MVC项目实践,在三层架构下实现SportsStore-01,EF Code First建模、DAL层等
SportsStore是<精通ASP.NET MVC3框架(第三版)>中演示的MVC项目,在该项目中涵盖了MVC的众多方面,包括:使用DI容器.URL优化.导航.分页.购物车.订单.产品管 ...
- 使用ABP框架踩过的坑系列1
企业级(例如ERP)应用, 一遍一遍的在重复:认证.验证.异常处理.日志.国际化和本地化.数据库连接管理.配置管理. 审计记录等,同时.NET有很多最佳实践:分层.模块化.DDD领域驱动.DI ...
随机推荐
- nodejs版实现properties后缀文件解析
1.propertiesParser.js let readline = require('readline'); let fs = require('fs'); // properties文件路径 ...
- 学习Golang语言(6):类型--切片
学习Golang语言(1): Hello World 学习Golang语言(2): 变量 学习Golang语言(3):类型--布尔型和数值类型 学习Golang语言(4):类型--字符串 学习Gola ...
- Android真机测试时无法连接服务器
之前服务器的通信一直是在模拟机上实现的,今天用在真机上却不成功.百度之后发现是安卓9以后禁止使用HTTP直接访问服务器.记录一下以后使用. 参考博文:https://blog.csdn.net/don ...
- spring controller 方法测试
controller 测试 不使用其他api接口测试工具 一般而言,我们写好一个模块后,会对其进行单元测试,再集成到现有的系统中. 但是呢~针对Controller.Service.Dao三层来说,我 ...
- Ionic创建混合App(二)
ionic 2 启动应用进入欢迎引导页 1.首先,使用CLI命令,创建引导页面 ionic g page welcome 2.需改welcome.html模板文件 <ion-slides pag ...
- WindowsForms使用Telerik Reporting
新建一个WindowsForms窗体项目 然后拖动ReportViewer这个控件到WindowsForms的窗体中 如上图所示,用来呈现报表的控件,这个控件可以打印报表,转换报表这类的功能 接下来我 ...
- /etc/nscd.conf - 域名服务缓存守护进程配置文件
描述 DESCRIPTION 该文件 /etc/nscd.conf 在启动 nscd(8) 时读入.每一行或者指定一个属性和值,或者指定一个属性.服务和一个值.域之间通过空格或者TAB分开.‘#’表示 ...
- python二维码模块(qrcode)
qrcode模块安装 运行命令行工具(cmd),使用pip安装工具分别安装qrcode. pip install qrcode 先来个简单的例子 import qrcode # 二维码内容 data ...
- Tengine 补充
Tengine 补充 开机启动 chkconfig --list chkconfig --add nginx chkconfig nginx on 时间问题 service ntpd status 虚 ...
- CSP-S 赛前模板复习
快读模板 这个连算法都算不上... inline int read() { int x=0,f=1; char ch=getchar(); while(ch<'0' || ch>'9') ...
