免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
ADO從數(shù)據(jù)集更新數(shù)據(jù)庫
此主題闡釋如何使用數(shù)據(jù)集來更新數(shù)據(jù)庫中的數(shù)據(jù)。還可使用 SqlCommand 直接在數(shù)據(jù)庫中插入、更新和刪除數(shù)據(jù),記住這一點(diǎn)很重要。理解從數(shù)據(jù)庫填充數(shù)據(jù)集中涉及的概念將有助于理解當(dāng)前的主題。 
“從數(shù)據(jù)庫填充數(shù)據(jù)集”中涉及的一些主題包括從數(shù)據(jù)庫檢索出數(shù)據(jù)并且將其放入數(shù)據(jù)集中,以及數(shù)據(jù)集是如何獨(dú)立于且不同于數(shù)據(jù)庫的。一旦加載了 DataSet,就可以修改數(shù)據(jù),并且數(shù)據(jù)集將跟蹤更改。

可 將 DataSet 視為從數(shù)據(jù)庫檢索出的數(shù)據(jù)的內(nèi)存內(nèi)緩存。DataSet 由表、關(guān)系和約束的集合組成。在此示例中,將說明如何在數(shù)據(jù)表 (DataTable) 上使用 Add 方法向數(shù)據(jù)集添加新數(shù)據(jù)。Add 方法使用一列所需的數(shù)據(jù)列或一個(gè)數(shù)據(jù)行 (DataRow)。 



// Create a new Connection and SqlDataAdapter

SqlConnection myConnection
= new SqlConnection("server=(local)\\VSdotNET;Trusted_Connection=yes;database=northwind");
SqlDataAdapter mySqlDataAdapter
= new SqlDataAdapter("Select * from Customers"
, myConnection);
DataSet myDataSet
= new
DataSet();
DataRow myDataRow;

//
Create command builder. This line automatically generates the update commands for you, so you don't
// have to provide or create your own.

SqlCommandBuilder mySqlCommandBuilder = new SqlCommandBuilder(mySqlDataAdapter);

//
Set the MissingSchemaAction property to AddWithKey because Fill will not cause primary
// key & unique key information to be retrieved unless AddWithKey is specified.

mySqlDataAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;

mySqlDataAdapter.Fill(myDataSet,
"Customers"
);

myDataRow
= myDataSet.Tables["Customers"
].NewRow();
myDataRow[
"CustomerId"] = "NewID"
;
myDataRow[
"ContactName"] = "New Name"
;
myDataRow[
"CompanyName"] = "New Company Name"
;

myDataSet.Tables[
"Customers"
].Rows.Add(myDataRow);

請(qǐng)注意數(shù)據(jù)表必須通過 NewRow 方法返回?cái)?shù)據(jù)行。該方法返回帶有適當(dāng)?shù)臄?shù)據(jù)表架構(gòu)的數(shù)據(jù)行對(duì)象。在將這個(gè)新的數(shù)據(jù)行添加到行集合 (RowsCollection) 之前,它是獨(dú)于表的。

可通過訪問數(shù)據(jù)行來更改數(shù)據(jù)行中的數(shù)據(jù)??梢允褂眯屑现械男兴饕?,該行索引通過 Rows 屬性來訪問:


myDataSet.Tables["Customers"].Rows[0]["ContactName"]="Peach";


還可通過主鍵值來訪問特定行:


DataRow myDataRow1 = myDataSet.Tables["Customers"].Rows.Find("ALFKI");
myDataRow1[
"ContactName"]="Peach";

此處,“ALFKI”是“Customers”表中主鍵“CustomerID”的值。使用 SqlDataAdapter 時(shí),從數(shù)據(jù)庫建立該鍵。如果不是在通過 PrimaryKey 屬性使用數(shù)據(jù)庫,則也可以對(duì)該鍵進(jìn)行設(shè)置。

使用 Delete 方法來移除行。請(qǐng)注意,數(shù)據(jù)集中發(fā)生的是邏輯刪除,只有將該數(shù)據(jù)集更新到數(shù)據(jù)庫時(shí),才會(huì)導(dǎo)致物理刪除。同樣地,可以在數(shù)據(jù)集上使用 RejectChanges,這種情況下將恢復(fù)該行。



myDataSet.Tables["Customers"].Rows[0].Delete();


行中保留了原值和新值。RowChanging 事件使您能夠同時(shí)訪問原值和新值,以決定是否繼續(xù)進(jìn)行編輯操作。由于保留了原值和新值,因此可以建立開放式鎖定和鍵更改等方案。

在將更改提交回?cái)?shù)據(jù)庫之前,需要設(shè)置 InsertCommand、UpdateCommand 和 DeleteCommand 來協(xié)調(diào)對(duì)數(shù)據(jù)庫做出的更改。對(duì)于有限的方案,可使用 SqlCommandBuilder 自動(dòng)生成這些命令,如以下示例中所示: 


SqlCommandBuilder mySqlCommandBuilder = new SqlCommandBuilder(mySqlDataAdapter);


要將數(shù)據(jù)從數(shù)據(jù)集提交到數(shù)據(jù)庫中,請(qǐng)使用 SqlDataAdapter 上的 Update 方法。


mySqlDataAdapter.Update(myDataSet, "Customers");


以下示例說明如何使用 SqlDataAdapter 從數(shù)據(jù)庫獲取數(shù)據(jù),在數(shù)據(jù)集中修改數(shù)據(jù),然后通過 SqlDataAdapter 將數(shù)據(jù)提交回?cái)?shù)據(jù)庫。

用CSHARP編寫代碼如下

Code
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
C#操作SQL Server數(shù)據(jù)庫
ADO.net
SqlCommandBuilder用法
使用DataGridView進(jìn)行增刪改查,并同步到數(shù)據(jù)庫
C#中用SqlDataAdapter修改數(shù)據(jù)庫中的表(源碼+注意的細(xì)節(jié))
C#連接SQL數(shù)據(jù)庫
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服