微軟發(fā)布的Data Access Application Block的使用代碼
1. 使用一般的sql語(yǔ)句進(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)用存儲(chǔ)過(guò)程進(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ù)項(xiàng)目需求做一下改動(dòng)再編譯成dll引入項(xiàng)目,以給項(xiàng)目開(kāi)發(fā)帶來(lái)便利. 下載地址如下:
http://download.microsoft.com/download/VisualStudioNET/daabref/RTM/NT5/EN-US/DataAccessApplicationBlock.msi
相關(guān)文章
.NET?Core實(shí)現(xiàn)簡(jiǎn)單的Redis?Client框架
本文詳細(xì)講解了.NET?Core實(shí)現(xiàn)簡(jiǎn)單的Redis?Client框架,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-02-02使用Aspose.Cells組件生成Excel文件實(shí)例
這篇文章主要介紹了使用Aspose.Cells組件生成Excel文件的方法,大家參考使用吧2013-11-11.NET使用報(bào)表工具FastReport實(shí)現(xiàn)打印功能
這篇文章介紹了.NET使用報(bào)表工具FastReport實(shí)現(xiàn)打印功能的方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-03-03asp.net類庫(kù)中添加WebService引用出現(xiàn)問(wèn)題解決方法
在Web項(xiàng)目?jī)?nèi)添加WebService的引用是件很簡(jiǎn)單的事情,不過(guò)對(duì)于一些新手朋友來(lái)說(shuō),就沒(méi)有那么簡(jiǎn)單了,因?yàn)樵谔砑拥倪^(guò)程中總會(huì)遇到一些困難,接下來(lái)詳細(xì)介紹如何解決,感興趣的你可不要錯(cuò)過(guò)了啊2013-02-02點(diǎn)擊提交按鈕后DropDownList的值變?yōu)槟J(rèn)值實(shí)現(xiàn)分析
在點(diǎn)擊提交按鈕后,頁(yè)面上所有的綁定到數(shù)據(jù)庫(kù)的控件值都恢復(fù)到默認(rèn)值,下面與大家分享下DropDownList的值變?yōu)槟J(rèn)值2013-05-05asp.net html控件的File控件實(shí)現(xiàn)多文件上傳實(shí)例分享
asp.net中html控件的File控件實(shí)現(xiàn)多文件上傳簡(jiǎn)單實(shí)例,開(kāi)發(fā)工具vs2010使用c#語(yǔ)言,感興趣的朋友可以了解下,必定是多文件上傳值得學(xué)習(xí),或許本文所提供的知識(shí)點(diǎn)對(duì)你有所幫助2013-02-02asp.net Repeater分頁(yè)實(shí)例(PageDataSource的使用)
Asp.net提供了三個(gè)功能強(qiáng)大的列表控件:DataGrid、DataList和Repeater控件,但其中只有DataGrid控件提供分頁(yè)功能。相對(duì)DataGrid,DataList和Repeater控件具有更高的樣式自定義性,所以很多時(shí)候我們喜歡使用DataList或Repeater控件來(lái)顯示數(shù)據(jù)2013-04-04ASP.NET中DropDownList和ListBox實(shí)現(xiàn)兩級(jí)聯(lián)動(dòng)功能
這篇文章主要介紹了ASP.NET中DropDownList和ListBox實(shí)現(xiàn)兩級(jí)聯(lián)動(dòng)功能的相關(guān)資料,需要的朋友可以參考下2016-01-01關(guān)于.NET/C#/WCF/WPF 打造IP網(wǎng)絡(luò)智能視頻監(jiān)控系統(tǒng)的介紹
本篇文章小編將為大家介紹,關(guān)于.NET/C#/WCF/WPF 打造IP網(wǎng)絡(luò)智能視頻監(jiān)控系統(tǒng)的介紹。需要的朋友參考下2013-04-04