微软发布的Data Access Application Block的使用代码
更新时间:2007年04月29日 00:00:00 作者:
为了方便的访问数据,微软自己封装了一个数据访问模块, 即Data Access Application Block. 通过它,我们用来访问数据库的编码量大大减少了. 这样的代码既有效率,又减少了出现错误的几率,其益处是可见的. 下面举两个例子比较一下
1. 使用一般的sql语句进行控件绑定, 常规代码如下:
1//Create the connection and sql to be executed
2string strConnTxt = "Server=(local);Database=Northwind;Integrated Security=True;";
3string strSql = "select * from Products where categoryid = 1"
4
5//Create and open the connection object
6SqlConnection objConn = new SqlConnection(strConnTxt);
7objConn.Open();
8
9//Create the connamd object
10SqlCommand objCmd = new SqlCommand(strSql, objConn);
11objCmd.CommandType = CommandType.Text;
12
13//databind the datagrid by calling the ExecuteReader() method
14DataGrid1.DataSource = objCmd.ExecuteReader();
15DataGrid1.DataBind();
16
17//close the connection
18objConn.Close();如果用微软封装的Data Access Application Block, 其主要是sqlHelper类,代码如下:
1//Create the connection string and sql to be executed
2string strSql = "select * from products where categoryid = 1";
3string strConnTxt = "Server=(local);Database=Northwind;Integrated Security=True;";
4
5DataGrid1.DataSource = SqlHelper.ExecuteReader(strConnTxt, CommandType.Text, strSql);
6DataGrid1.DataBind();
2. 调用存储过程进行控件绑定
常规代码如下:
1//Open a connection to Northwind
2SqlConnection objConn = new SqlConnection("Server=(local);Database=Northwind;Integrated Security=True;");
3ObjConn.Open();
4
5//Create the stored procedure command object
6SqlCommand objCmd = new SqlCommand("getProductsCategory", objConn);
7objCmd.CommandType = CommandType.StoredProcedure;
8
9//create the parameter object for the stored procedure parameter
10objCmd.Parameter.Add("@CategoryID", SqlDbType.Int);
11objCmd.Parameter["@CategoryID"].Value = 1;
12
13//create our DataAdapter and DataSet objects
14SqlDataAdapter objDA = new SqlDataAdapter(objCmd);
15DataSet objDS = new DataSet("Category_Results");
16
17//fill the dataset
18objDA.Fill(objDS);
19
20//databind the datagrid
21DataGrid1.DataSource = objDS;
22DataGrid1.DataBind();
23
24//close connection
25objConn.Close();如果用微软封装的Data Access Application Block,其主要是sqlHelper类,代码如下:
1string strConn = "Server=(local);Database=Northwind;Integrated Security=True;";
2DataSet objDS = SqlHelper.ExecuteDataset(strConn, CommandType.StoredProcedure, "getProductsByCategory", new SqlParameter("@CategoryID", 1));
3
4DataGrid1.DataSource = objDS;
5DataGrid1.DataBind();
Data Access Application Block, 有其封装的源代码和帮助文件,我们也可以根据项目需求做一下改动再编译成dll引入项目,以给项目开发带来便利. 下载地址如下:
http://download.microsoft.com/download/VisualStudioNET/daabref/RTM/NT5/EN-US/DataAccessApplicationBlock.msi
1. 使用一般的sql语句进行控件绑定, 常规代码如下:
1//Create the connection and sql to be executed
2string strConnTxt = "Server=(local);Database=Northwind;Integrated Security=True;";
3string strSql = "select * from Products where categoryid = 1"
4
5//Create and open the connection object
6SqlConnection objConn = new SqlConnection(strConnTxt);
7objConn.Open();
8
9//Create the connamd object
10SqlCommand objCmd = new SqlCommand(strSql, objConn);
11objCmd.CommandType = CommandType.Text;
12
13//databind the datagrid by calling the ExecuteReader() method
14DataGrid1.DataSource = objCmd.ExecuteReader();
15DataGrid1.DataBind();
16
17//close the connection
18objConn.Close();如果用微软封装的Data Access Application Block, 其主要是sqlHelper类,代码如下:
1//Create the connection string and sql to be executed
2string strSql = "select * from products where categoryid = 1";
3string strConnTxt = "Server=(local);Database=Northwind;Integrated Security=True;";
4
5DataGrid1.DataSource = SqlHelper.ExecuteReader(strConnTxt, CommandType.Text, strSql);
6DataGrid1.DataBind();
2. 调用存储过程进行控件绑定
常规代码如下:
1//Open a connection to Northwind
2SqlConnection objConn = new SqlConnection("Server=(local);Database=Northwind;Integrated Security=True;");
3ObjConn.Open();
4
5//Create the stored procedure command object
6SqlCommand objCmd = new SqlCommand("getProductsCategory", objConn);
7objCmd.CommandType = CommandType.StoredProcedure;
8
9//create the parameter object for the stored procedure parameter
10objCmd.Parameter.Add("@CategoryID", SqlDbType.Int);
11objCmd.Parameter["@CategoryID"].Value = 1;
12
13//create our DataAdapter and DataSet objects
14SqlDataAdapter objDA = new SqlDataAdapter(objCmd);
15DataSet objDS = new DataSet("Category_Results");
16
17//fill the dataset
18objDA.Fill(objDS);
19
20//databind the datagrid
21DataGrid1.DataSource = objDS;
22DataGrid1.DataBind();
23
24//close connection
25objConn.Close();如果用微软封装的Data Access Application Block,其主要是sqlHelper类,代码如下:
1string strConn = "Server=(local);Database=Northwind;Integrated Security=True;";
2DataSet objDS = SqlHelper.ExecuteDataset(strConn, CommandType.StoredProcedure, "getProductsByCategory", new SqlParameter("@CategoryID", 1));
3
4DataGrid1.DataSource = objDS;
5DataGrid1.DataBind();
Data Access Application Block, 有其封装的源代码和帮助文件,我们也可以根据项目需求做一下改动再编译成dll引入项目,以给项目开发带来便利. 下载地址如下:
http://download.microsoft.com/download/VisualStudioNET/daabref/RTM/NT5/EN-US/DataAccessApplicationBlock.msi
相关文章
asp.net类库中添加WebService引用出现问题解决方法
在Web项目内添加WebService的引用是件很简单的事情,不过对于一些新手朋友来说,就没有那么简单了,因为在添加的过程中总会遇到一些困难,接下来详细介绍如何解决,感兴趣的你可不要错过了啊2013-02-02点击提交按钮后DropDownList的值变为默认值实现分析
在点击提交按钮后,页面上所有的绑定到数据库的控件值都恢复到默认值,下面与大家分享下DropDownList的值变为默认值2013-05-05asp.net html控件的File控件实现多文件上传实例分享
asp.net中html控件的File控件实现多文件上传简单实例,开发工具vs2010使用c#语言,感兴趣的朋友可以了解下,必定是多文件上传值得学习,或许本文所提供的知识点对你有所帮助2013-02-02asp.net Repeater分页实例(PageDataSource的使用)
Asp.net提供了三个功能强大的列表控件:DataGrid、DataList和Repeater控件,但其中只有DataGrid控件提供分页功能。相对DataGrid,DataList和Repeater控件具有更高的样式自定义性,所以很多时候我们喜欢使用DataList或Repeater控件来显示数据2013-04-04ASP.NET中DropDownList和ListBox实现两级联动功能
这篇文章主要介绍了ASP.NET中DropDownList和ListBox实现两级联动功能的相关资料,需要的朋友可以参考下2016-01-01关于.NET/C#/WCF/WPF 打造IP网络智能视频监控系统的介绍
本篇文章小编将为大家介绍,关于.NET/C#/WCF/WPF 打造IP网络智能视频监控系统的介绍。需要的朋友参考下2013-04-04
最新评论