微软发布的Data Access Application Block的使用代码
程序员文章站
2023-11-09 17:33:34
为了方便的访问数据,微软自己封装了一个数据访问模块, 即data access application block. 通过...
为了方便的访问数据,微软自己封装了一个数据访问模块, 即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
上一篇: SQL Server 索引介绍