微软发布的Data Access Application Block的使用代码

所属分类: 网络编程 / ASP.NET 阅读数: 745
收藏 0 赞 0 分享
为了方便的访问数据,微软自己封装了一个数据访问模块, 即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
更多精彩内容其他人还在看

运行page页面时的事件执行顺序及页面的回发与否深度了解

page页面时的事件执行顺序的了解对于一些.net开发者起到者尤关重要的作用;页面的回发与否会涉及到某些事件执行与不执行,在本文中会详细介绍,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

Web.config(应用程序的配置信息)总结

Web.config文件是一个XML文本文件,它用来储存 ASP.NET Web 应用程序的配置信息(如最常用的设置ASP.NET Web 应用程序的身份验证方式),它可以出现在应用程序的每一个目录中,接下来详细介绍一下配置情况,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

C#时间格式化(Datetime)用法详解

C#时间格式化Datetime.ToString参数format格式详细用法,本文将进行介绍,感兴趣的朋友可以了解下啊
收藏 0 赞 0 分享

实现onmouseover和onmouseout应用于RadioButtonList或CheckBoxList控件上

一直想实现onmouseover和onmouseout应用于RadioButtonList或CheckBoxList控件上。此功能就是当鼠标经过时RadioButtonList或CheckBoxList每一个Item时,让Item有特效显示,离开时,恢复原样有演示动画,感兴趣的朋
收藏 0 赞 0 分享

Asp.net 图片文件防盗链(尊重劳动成果)及BeginRequest事件学习

关于图片盗链这个问题,毕竟是自己的劳动成功,很多人不希望别人就那么轻易地偷走了;反盗链的程序其实很简单,熟悉ASP.NET 应用程序生命周期的话很容易就可以写一个,运用HttpModule在BeginRequest事件中拦截请求就ok了
收藏 0 赞 0 分享

asp.net Grid 导出Excel实现程序代码

看了FineUI中的将Grid导出为Excel的实现方法,实际上是可以非常简单。看来很难的问题,变换一种思路就可以非常简单
收藏 0 赞 0 分享

使用C#处理WebBrowser控件在不同域名中的跨域问题

我们在做web测试时,经常会使用WebBrowser来进行一些自动化的任务而有些网页上面会用IFrame去嵌套别的页面,这些页面可能不是在相同域名下的,这时就会出现跨域问题,无法直接在WebBrowser中获取到IFrame中的元素,接下来介绍如何解决此问题,需要了解的朋友可以参
收藏 0 赞 0 分享

.NET 下运用策略模式(组合行为和实体的一种模式)

我简单的理解策略模式就是把行为(方法)单独的抽象出来,并采用组合(Has-a)的方式,来组合行为和实体的一种模式比如,.NET中对数组排序的Sort的方法就是一个策略模式的实现模板
收藏 0 赞 0 分享

使用Entity Framework(4.3.1版本)遇到的问题整理

在这里记录一下之前使用Entity Framework(4.3.1版本)遇到的问题:更新没有设置主键的表、更改Code-First的默认连接、检测字符串截断错误,需要的朋友可以参考下
收藏 0 赞 0 分享

Asp.net利用JQuery AJAX实现无刷新评论思路与代码

Asp.net利用JQuery AJAX实现无刷新评论,此功能是每一个从事asp.net开发者的朋友都希望实现的,本文利用闲暇时间整理了一些,有需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多