SqlCommandBuilder的作用

來源:互聯網
上載者:User

標籤:style   color   os   使用   io   ar   資料   cti   代碼   

最近看一個案例,關於如何向資料庫傳資料,頗有發現。

以前我的資料都是採用Sql代碼用SqlCommand一條一條語句的傳入資料庫。

但是今天發現有更好的方法。

代碼如下:

SqlConnection cn=new SqlConnection(ConStr);

   DataSet DS=new DataSet();

   SqlDataAdapter Sda=new SqlDataAdapter("Select * from Table1",cn);

   try

   {

    Sda.Fill(DS,"Table1");

    MessageBox.Show(DS.Tables[0].Rows.Count.ToString());

   }

   catch(Exception er)

   {

    MessageBox.Show(er.Message);

   }

   //DataSet DS2=DS.Clone();

   //Sda.SelectCommand=new SqlCommand("Select * from Table1");

   DataRow dr=DS.Tables[0].NewRow();

   dr["F1"]=5;

   dr["F2"]=5;

   dr["F3"]=5;

   DS.Tables[0].Rows.Add(dr);

   dr=DS.Tables[0].NewRow();

   dr["F1"]=6;

   dr["F2"]=6;

   dr["F3"]=6;

   DS.Tables[0].Rows.Add(dr);

   

   SqlCommandBuilder SCB=new SqlCommandBuilder(Sda);

   

   try

   {

    Sda.Update(DS,"Table1");

   }

   catch(Exception er)

   {

    MessageBox.Show(er.Message);

   }

  

   

   Sda.Dispose();

   cn.Close();

資料庫用的是SQL2000,先在資料庫中建立Table1的暫存資料表,建立欄位MainID,F1,F2,F3

初始資料如下

MainID     F1    F2    F3

1                1        1    1

2                2        2    2

3                3        3    3

4                4        4    4

注意到,我先把資料從資料庫中讀出來,賦值到DS中

結果顯然為4行

然後我在該資料集DS中添加2個同構行

這是注意了,我沒有做任何的Insert相關語句

打算直接採用SqlDataAdapter.Update(DataSet,TableScr)方法

但是注意,直接用是不行的,會提示缺少Insert命令的關聯

這是可以用了如黃色高亮標記的語句

SqlCommandBuilder SCB=new SqlCommandBuilder(Sda);

這是所有關聯就都建立好了,全自動。奇妙,詭異阿

這樣就方便多了。

恩,更多的相關功能正在研究中。

 =============================================================

使用SqlCommandBuilder與SqlDataAdapter結合使用,可以方便地去資料庫進行更新。只要指定Select 語句就可以自動產生Insert,update,delete語句,但要注意一點。Select 語句中返回的列要包括主鍵列,否則將無法產生Update,和Delete語句。相應的操作將無法執行

public static DataSet SelectSqlSrvRows(string myConnection, string mySelectQuery, string myTableName)

{

   SqlConnection myConn = new SqlConnection(myConnection);

   SqlDataAdapter myDataAdapter = new SqlDataAdapter();

   myDataAdapter.SelectCommand = new SqlCommand(mySelectQuery, myConn);

   SqlCommandBuilder cb = new SqlCommandBuilder(myDataAdapter);

   myConn.Open();

   DataSet ds = new DataSet();

   myDataAdapter.Fill(ds, myTableName);

   //code to modify data in DataSet here

   //Without the SqlCommandBuilder this line would fail

   myDataAdapter.Update(ds, myTableName);

   myConn.Close();

   return ds;

}

SqlCommandBuilder的作用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.