小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

ADO從數(shù)據(jù)集更新數(shù)據(jù)庫(kù)

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

可 將 DataSet 視為從數(shù)據(jù)庫(kù)檢索出的數(shù)據(jù)的內(nèi)存內(nèi)緩存。DataSet 由表、關(guān)系和約束的集合組成。在此示例中,將說(shuō)明如何在數(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ù)表必須通過(guò) NewRow 方法返回?cái)?shù)據(jù)行。該方法返回帶有適當(dāng)?shù)臄?shù)據(jù)表架構(gòu)的數(shù)據(jù)行對(duì)象。在將這個(gè)新的數(shù)據(jù)行添加到行集合 (RowsCollection) 之前,它是獨(dú)于表的。

可通過(guò)訪(fǎng)問(wèn)數(shù)據(jù)行來(lái)更改數(shù)據(jù)行中的數(shù)據(jù)??梢允褂眯屑现械男兴饕?,該行索引通過(guò) Rows 屬性來(lái)訪(fǎng)問(wèn):


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


還可通過(guò)主鍵值來(lái)訪(fǎng)問(wèn)特定行:


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

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

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



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


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

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


SqlCommandBuilder mySqlCommandBuilder = new SqlCommandBuilder(mySqlDataAdapter);


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


mySqlDataAdapter.Update(myDataSet, "Customers");


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

用CSHARP編寫(xiě)代碼如下

Code

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶(hù)發(fā)布,不代表本站觀(guān)點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶(hù) 評(píng)論公約

    類(lèi)似文章 更多