服務(wù)器端數(shù)據(jù)訪問
2024-07-21 02:23:47
供稿:網(wǎng)友
菜鳥學(xué)堂:
服務(wù)器端數(shù)據(jù)訪問
服務(wù)器端數(shù)據(jù)介紹
連接、命令和數(shù)據(jù)集
訪問基于 sql 的數(shù)據(jù)
將 sql 數(shù)據(jù)綁定到 datagrid
執(zhí)行參數(shù)化選擇
在 sql 數(shù)據(jù)庫中插入數(shù)據(jù)
更新 sql 數(shù)據(jù)庫中的數(shù)據(jù)
刪除 sql 數(shù)據(jù)庫中的數(shù)據(jù)
將 sql 數(shù)據(jù)庫中的數(shù)據(jù)排序
處理主-從關(guān)系
編寫和使用存儲過程
訪問基于 xml 的數(shù)據(jù)
本節(jié)小結(jié)
服務(wù)器端數(shù)據(jù)介紹
數(shù)據(jù)訪問是任何實際應(yīng)用程序的核心部分,而 asp.net 提供了一套豐富的控件,這些控件與公共語言運行庫中提供的托管數(shù)據(jù)訪問 api 很好地集成在一起。 本節(jié)多次演練同一個示例,該示例使用 asp.net datagrid 控件綁定到 sql 查詢的結(jié)果和 xml 數(shù)據(jù)文件。 本節(jié)假定您熟悉數(shù)據(jù)庫基礎(chǔ)知識和 sql 查詢語言。
服務(wù)器端數(shù)據(jù)訪問很獨特,因為 web 頁基本上是無狀態(tài)的。當試圖執(zhí)行事務(wù)時,如插入或更新從數(shù)據(jù)庫檢索的數(shù)據(jù)集中的記錄時,這向我們提出了某些困難的挑戰(zhàn)。 正如將在本節(jié)中看到的,datagrid 控件可以幫助應(yīng)付這些挑戰(zhàn),使您得以更多地集中在應(yīng)用程序邏輯上,對狀態(tài)管理和事件處理的具體細節(jié)則不用考慮太多。
連接、命令和數(shù)據(jù)集
公共語言運行庫為數(shù)據(jù)密集的應(yīng)用程序開發(fā)提供了完整的托管數(shù)據(jù)訪問 api 集。 這些 api 幫助抽象數(shù)據(jù)并用一致的方法表示數(shù)據(jù),與實際的數(shù)據(jù)源(sql server、oledb、xml 等)無關(guān)。 最常使用的對象基本上有三種:連接、命令和數(shù)據(jù)集。
連接表示與某些數(shù)據(jù)存儲區(qū)(如 sql server 或 xml 文件)的物理連接。
命令表示從數(shù)據(jù)存儲區(qū)檢索(選擇)或?qū)?shù)據(jù)存儲區(qū)進行操作(插入、更新、刪除)的指令。
數(shù)據(jù)集表示應(yīng)用程序使用的實際數(shù)據(jù)。 注意,數(shù)據(jù)集總是同它們的源連接和數(shù)據(jù)模型斷開并可獨立修改。 不過,數(shù)據(jù)集的更改可以很容易與起始數(shù)據(jù)模型相協(xié)調(diào)。
有關(guān)公共語言運行庫中托管數(shù)據(jù)訪問解決方案的更詳細演練,請閱讀本教程的 ado.net 概述一節(jié)。
訪問基于 sql 的數(shù)據(jù)
應(yīng)用程序一般需要對 sql 數(shù)據(jù)庫執(zhí)行一個或多個選擇、插入、更新或刪除查詢。 下表顯示上述每個查詢的示例。
查詢
示例
簡單選擇
select * from employees where firstname = 'bradley';
聯(lián)接選擇
select * from employees e, managers m where e.firstname = m.firstname;
插入
insert into employees values ('123-45-6789','bradley','millington','program manager');
更新
update employees set title = 'development lead' where firstname = 'bradley';
刪除
delete from employees where productivity < 10;
為了使頁能夠訪問執(zhí)行 sql 數(shù)據(jù)訪問所需的類,必須將 system.data 和 system.data.sqlclient 命名空間導(dǎo)入到頁中。
<%@ import namespace="system.data" %> <%@ import namespace="system.data.sqlclient" %>
若要對 sql 數(shù)據(jù)庫執(zhí)行選擇查詢,請創(chuàng)建與數(shù)據(jù)庫的 sqlconnection,傳遞連接字符串,然后構(gòu)造包含查詢語句的 sqldataadapter 對象。 若要用查詢結(jié)果填充 dataset 對象,請調(diào)用命令的 fill 方法。
sqlconnection myconnection = new sqlconnection("server=(local)/netsdk;database=pubs;trusted_connection=yes"); sqldataadapter mycommand = new sqldataadapter("select * from authors", myconnection); dataset ds = new dataset(); mycommand.fill(ds, "authors"); dim myconnection as new sqlconnection("server=(local)/netsdk;database=pubs;trusted_connection=yes") dim mycommand as new sqldataadapter("select * from authors", myconnection) dim ds as new dataset() mycommand.fill(ds, "authors") var myconnection:sqlconnection = new sqlconnection("server=(local)/netsdk;database=pubs;trusted_connection=yes"); var mycommand:sqldataadapter = new sqldataadapter("select * from authors", myconnection); var ds:dataset = new dataset(); mycommand.fill(ds, "authors");
正如本節(jié)前面所提到的,使用數(shù)據(jù)集的好處是它為您提供了斷開連接的數(shù)據(jù)庫視圖。 可以在應(yīng)用程序中操作數(shù)據(jù)集,然后在以后協(xié)調(diào)更改和實際的數(shù)據(jù)庫。 對于長期運行的應(yīng)用程序,這通常是最好的方法。 對于 web 應(yīng)用程序,通常對每個請求執(zhí)行短操作(一般只是顯示數(shù)據(jù))。 通常不需要在一系列請求間保持 dataset 對象。 對于這類情況,可以使用 sqldatareader。
sqldatareader 對從 sql 數(shù)據(jù)庫檢索的數(shù)據(jù)提供僅向前的只讀指針。 因為 sqldatareader 使用表格數(shù)據(jù)流 (tds) 直接從數(shù)據(jù)庫連接讀取數(shù)據(jù),因此它如果可以用于方案,其執(zhí)行效率會比 dataset 高。
若要使用 sqldatareader,請聲明 sqlcommand 而不是 sqldataadapter。 sqlcommand 公開返回 sqldatareader 的 executereader 方法。 還請注意,當使用 sqlcommand 時,必須顯式打開和關(guān)閉 sqlconnection。 調(diào)用 executereader 后,sqldatareader 可以綁定到 asp.net 服務(wù)器控件,正如將在下一節(jié)看到的。
<tab name="c#">
sqlconnection myconnection = new sqlconnection("server=(local)/netsdk;database=pubs;trusted_connection=yes"); sqlcommand mycommand = new sqlcommand("select * from authors", myconnection);
myconnection.open();
sqldatareader dr = mycommand.executereader();
...
myconnection.close();
</tab>
<tab name="vb">
dim myconnection as sqlconnection = new sqlconnection("server=(local)/netsdk;database=pubs;trusted_connection=yes") dim mycommand as sqlcommand = new sqlcommand("select * from authors", myconnection)
myconnection.open()
dim dr as sqldatareader = mycommand.executereader()
...
myconnection.close()
</tab>
<tab name="jscript">
var myconnection:sqlconnection = new sqlconnection("server=(local)/netsdk;database=pubs;trusted_connection=yes"); var mycommand:sqlcommand = new sqlcommand("select * from authors", myconnection);
myconnection.open();
var dr : sqldatareader; dr = mycommand.executereader();
...
myconnection.close();
</tab>
當執(zhí)行不要求返回數(shù)據(jù)的命令(如插入、更新和刪除)時,也使用 sqlcommand。 該命令通過調(diào)用 executenonquery 方法發(fā)出,而此方法返回受影響的行數(shù)。 注意當使用 sqlcommand 時,必須顯式打開連接;sqldataadapter 自動為您處理如何打開連接。
<tab name="c#"> sqlconnection myconnection = new sqlconnection("server=(local)/netsdk;database=pubs;trusted_connection=yes"); sqlcommand mycommand = new sqlcommand( "update authors set phone='(800) 555-5555' where au_id = '123-45-6789'", myconnection);
mycommand.connection.open(); mycommand.executenonquery(); mycommand.connection.close();
</tab>
<tab name="vb"> dim myconnection as new sqlconnection("server=(local)/netsdk;database=pubs;trusted_connection=yes") dim mycommand as new sqlcommand( _ "update authors set phone='(800) 555-5555' where au_id = '123-45-6789'", _ myconnection)
mycommand.connection.open() mycommand.executenonquery() mycommand.connection.close()
</tab>
<tab name="jscript"> var myconnection:sqlconnection = new sqlconnection("server=(local)/netsdk;database=pubs;trusted_connection=yes"); var mycommand:sqlcommand = new sqlcommand( "update authors set phone='(800) 555-5555' where au_id = '123-45-6789'", myconnection);
mycommand.connection.open(); mycommand.executenonquery(); mycommand.connection.close();
</tab>
重要說明:始終記住在頁完成執(zhí)行之前關(guān)閉與數(shù)據(jù)模型的連接。 如果不關(guān)閉連接,則可能會在等待頁實例被垃圾回收處理期間不經(jīng)意地超過連接限制。
將 sql 數(shù)據(jù)綁定到 datagrid
下面的示例顯示一個綁定到 datagrid 控件的簡單選擇查詢。 datagrid 呈現(xiàn)包含 sql 數(shù)據(jù)的表。