简介微软发布的Data Access Application Block
2007-04-08 17:22
423 查看
为了方便的访问数据,微软自己封装了一个数据访问模块, 即Data Access Application Block. 通过它,我们用来访问数据库的编码量大大减少了. 这样的代码既有效率,又减少了出现错误的几率,其益处是可见的. 下面举两个例子比较一下
1. 使用一般的sql语句进行控件绑定, 常规代码如下:
//Create the connection and sql to be executed
string strConnTxt = "Server=(local);Database=Northwind;Integrated Security=True;";
string strSql = "select * from Products where categoryid = 1"
//Create and open the connection object
SqlConnection objConn = new SqlConnection(strConnTxt);
objConn.Open();
/Create the connamd object
SqlCommand objCmd = new SqlCommand(strSql, objConn);
objCmd.CommandType = CommandType.Text;
//databind the datagrid by calling the ExecuteReader() method
DataGrid1.DataSource = objCmd.ExecuteReader();
DataGrid1.DataBind();
//close the connection
objConn.Close();
如果用微软封装的Data Access Application Block, 其主要是sqlHelper类,代码如下:
//Create the connection string and sql to be executed
string strSql = "select * from products where categoryid = 1";
string strConnTxt = "Server=(local);Database=Northwind;Integrated Security=True;";
DataGrid1.DataSource = SqlHelper.ExecuteReader(strConnTxt, CommandType.Text, strSql);
DataGrid1.DataBind();
2. 调用存储过程进行控件绑定
常规代码如下:
//Open a connection to Northwind
SqlConnection objConn = new SqlConnection("Server=(local);Database=Northwind;Integrated Security=True;");
ObjConn.Open();
//Create the stored procedure command object
SqlCommand objCmd = new SqlCommand("getProductsCategory", objConn);
objCmd.CommandType = CommandType.StoredProcedure;
//create the parameter object for the stored procedure parameter
objCmd.Parameter.Add("@CategoryID", SqlDbType.Int);
objCmd.Parameter["@CategoryID"].Value = 1;
//create our DataAdapter and DataSet objects
SqlDataAdapter objDA = new SqlDataAdapter(objCmd);
DataSet objDS = new DataSet("Category_Results");
//fill the dataset
objDA.Fill(objDS);
//databind the datagrid
DataGrid1.DataSource = objDS;
DataGrid1.DataBind();
//close connection
objConn.Close();
如果用微软封装的Data Access Application Block,其主要是sqlHelper类,代码如下:
string strConn = "Server=(local);Database=Northwind;Integrated Security=True;";
DataSet objDS = SqlHelper.ExecuteDataset(strConn, CommandType.StoredProcedure, "getProductsByCategory", new SqlParameter("@CategoryID", 1));
DataGrid1.DataSource = objDS;
DataGrid1.DataBind();
Data Access Application Block, 有其封装的源代码和帮助文件,我们也可以根据项目需求做一下改动再编译成dll引入项目,以给项目开发带来便利. 下载地址如下:
http://download.microsoft.com/download/VisualStudioNET/daabref/RTM/NT5/EN-US/DataAccessApplicationBlock.msi
1. 使用一般的sql语句进行控件绑定, 常规代码如下:
//Create the connection and sql to be executed
string strConnTxt = "Server=(local);Database=Northwind;Integrated Security=True;";
string strSql = "select * from Products where categoryid = 1"
//Create and open the connection object
SqlConnection objConn = new SqlConnection(strConnTxt);
objConn.Open();
/Create the connamd object
SqlCommand objCmd = new SqlCommand(strSql, objConn);
objCmd.CommandType = CommandType.Text;
//databind the datagrid by calling the ExecuteReader() method
DataGrid1.DataSource = objCmd.ExecuteReader();
DataGrid1.DataBind();
//close the connection
objConn.Close();
如果用微软封装的Data Access Application Block, 其主要是sqlHelper类,代码如下:
//Create the connection string and sql to be executed
string strSql = "select * from products where categoryid = 1";
string strConnTxt = "Server=(local);Database=Northwind;Integrated Security=True;";
DataGrid1.DataSource = SqlHelper.ExecuteReader(strConnTxt, CommandType.Text, strSql);
DataGrid1.DataBind();
2. 调用存储过程进行控件绑定
常规代码如下:
//Open a connection to Northwind
SqlConnection objConn = new SqlConnection("Server=(local);Database=Northwind;Integrated Security=True;");
ObjConn.Open();
//Create the stored procedure command object
SqlCommand objCmd = new SqlCommand("getProductsCategory", objConn);
objCmd.CommandType = CommandType.StoredProcedure;
//create the parameter object for the stored procedure parameter
objCmd.Parameter.Add("@CategoryID", SqlDbType.Int);
objCmd.Parameter["@CategoryID"].Value = 1;
//create our DataAdapter and DataSet objects
SqlDataAdapter objDA = new SqlDataAdapter(objCmd);
DataSet objDS = new DataSet("Category_Results");
//fill the dataset
objDA.Fill(objDS);
//databind the datagrid
DataGrid1.DataSource = objDS;
DataGrid1.DataBind();
//close connection
objConn.Close();
如果用微软封装的Data Access Application Block,其主要是sqlHelper类,代码如下:
string strConn = "Server=(local);Database=Northwind;Integrated Security=True;";
DataSet objDS = SqlHelper.ExecuteDataset(strConn, CommandType.StoredProcedure, "getProductsByCategory", new SqlParameter("@CategoryID", 1));
DataGrid1.DataSource = objDS;
DataGrid1.DataBind();
Data Access Application Block, 有其封装的源代码和帮助文件,我们也可以根据项目需求做一下改动再编译成dll引入项目,以给项目开发带来便利. 下载地址如下:
http://download.microsoft.com/download/VisualStudioNET/daabref/RTM/NT5/EN-US/DataAccessApplicationBlock.msi
相关文章推荐
- 简介微软发布的Data Access Application Block
- 简介微软发布的Data Access Application Block
- 简介微软发布的Data Access Application Block
- 简介微软发布的Data Access Application Block
- 简介微软发布的Data Access Application Block
- 微软发布的Data Access Application Block的使用代码
- 微软发布的Data Access Application Block的使用代码
- [EntLib]微软企业库6 基于Data Access Application Block的Repository
- [EntLib]微软企业库6 Data Access Application Block 扩展
- 数据访问应用程序块 (Data Access Application Block) .NET 组件简介
- [原创]我的ORM: 开发自己的Data Access Application Block - Part I
- Enterprise Library Library Data Access Application Block Step By Step
- 针对Data Access Application Block的生成DBCommandWrapper的代码生成器
- Enterprise Library - Data Access Application Block 6.0.1304
- Data Access Application Block for .NET
- 感觉 Data Access Application Block(DAAB) 里也有可能写得不太好的地方
- 用Enterprise Library - January 2006 的Data Access Application Block 访问Oracle存储过程心得
- Data Access Application Block for .NET
- Enterprise Library – Data Access Application Block - Part 1
- 2 - Much ADO about Data Access: Using the Data Access Application Block