国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

微軟發(fā)布的Data Access Application Block的使用代碼

2020-01-18 01:41:00
字體:
供稿:網(wǎng)友
為了方便的訪問數(shù)據(jù),微軟自己封裝了一個數(shù)據(jù)訪問模塊, 即Data Access Application Block. 通過它,我們用來訪問數(shù)據(jù)庫的編碼量大大減少了. 這樣的代碼既有效率,又減少了出現(xiàn)錯誤的幾率,其益處是可見的. 下面舉兩個例子比較一下

1. 使用一般的sql語句進(jìn)行控件綁定, 常規(guī)代碼如下:


 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. 調(diào)用存儲過程進(jìn)行控件綁定
常規(guī)代碼如下:

 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, 有其封裝的源代碼和幫助文件,我們也可以根據(jù)項目需求做一下改動再編譯成dll引入項目,以給項目開發(fā)帶來便利. 下載地址如下:
http://download.microsoft.com/download/VisualStudioNET/daabref/RTM/NT5/EN-US/DataAccessApplicationBlock.msi
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 太仓市| 广州市| 鹤岗市| 淳安县| 仁寿县| 浏阳市| 凤庆县| 来凤县| 杂多县| 理塘县| 南召县| 丰城市| 通山县| 浮山县| 班戈县| 松原市| 舒城县| 滨州市| 崇义县| 灵寿县| 射洪县| 东海县| 白银市| 云南省| 浮山县| 辉县市| 磴口县| 方城县| 泾川县| 怀仁县| 葫芦岛市| 岳阳县| 平邑县| 大田县| 康定县| 射阳县| 新安县| 建宁县| 那曲县| 合肥市| 项城市|