乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

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

       旭龍 2011-01-25
      此主題闡釋如何使用數(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

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(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)遵守用戶 評(píng)論公約

        類似文章 更多