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

所属分类: 网络编程 / ASP.NET 阅读数: 755
收藏 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
更多精彩内容其他人还在看

ASP.NET Web API教程 创建Admin视图详细介绍

现在我们转入客户端,并添加一个能够使用从Admin控制器而来的数据的页面。通过给控制器发送AJAX请求的方式,该页面将允许用户创建、编辑,或删除产品
收藏 0 赞 0 分享

asp.net中session的原理及应用详解

Session是一种Web会话中的常用状态之一,Session提供了一种把信息保存在服务器内存中的方式。他能储存任何数据类型,包含自定义对象,本文将详细介绍asp.net中session的原理及应用,需要的朋友可以参考下
收藏 0 赞 0 分享

Web开发异常行为排查常用方法图文介绍

平常程序遇到错误,开发环境下一般都用调试搞定,生产环境下通过查看日志搞定。但也有搞不定的时候,本文提供了详细的解决方案
收藏 0 赞 0 分享

ASP.NET repeater添加序号列的方法

在项目开发过程中,会经常遇到ASP.NET repeater控件添加序号列,有些新手可能还不会,网上搜集整理了一些,需要的朋友可以参考下
收藏 0 赞 0 分享

GridView常用操作事件图文介绍

对于gridview学NET的同学再熟悉不过,但是其中功能事件是否能编码熟练实现
收藏 0 赞 0 分享

批量账号的login测试功能实现

用WaitiN写了个简单的login自动化测试,能够使用少量的代码实现批量账号的login测试,需要的朋友可以参考下
收藏 0 赞 0 分享

iis配置asp.net常见问题解决方案

如何解决iis配置asp.net常见一些问题,根据自己的使用经验,总结了一些,希望可以帮助你问
收藏 0 赞 0 分享

.net自带的库生成zip文件的方法

平时我们创建Zip文件的时候,要么用现成的软件,要么用第三方的开源库。其实用.net自带的类操作起来也非常方便
收藏 0 赞 0 分享

asp.net中Post表单保存页面状态并输出源码的实现方法

先执行脚本,复制源码到隐藏域里,再输出源码,注意代码红色设置
收藏 0 赞 0 分享

用Html5与Asp.net MVC上传多个文件的实现代码

Html 5 的有一些File API,对Form表单增强的特性,让我们轻松支持多文件上传,看下面的Html片断代码
收藏 0 赞 0 分享
查看更多