日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

SqlCommandBuilder

發(fā)布時間:2024/9/5 编程问答 48 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SqlCommandBuilder 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

SqlCommandBuilder


作用:c#中用來批量更新數(shù)據(jù)庫   用法:一般和adapter結(jié)合使用。   例:   SqlConnection conn = new SqlConnection(strConnection));//連接數(shù)據(jù)庫   SqlDataAdapter myAdapter = new SqlDataAdapter();//new一個adapter對象   SqlCommand myCommand = new SqlCommand("select * from "+strTblName),(SqlConnection) conn); //cmd   SqlCommandBuilder myCommandBuilder = new SqlCommandBuilder(myAdapter); //new 一個 SqlCommandBuilder   myAdapter.InsertCommand = myCommandBuilder .GetInsertCommand();//插入   myAdapter.UpdateCommand = myCommandBuilder .GetUpdateCommand();//更新   myAdapter.DeleteCommand = myCommandBuilder .GetDeleteCommand();//刪除   conn.Open();//打開數(shù)據(jù)庫   myAdapter.Update(ds); //更新ds到數(shù)據(jù)庫   conn.Close();//關閉數(shù)據(jù)庫   何時使用:   a. 有時候需要緩存的時候,比如說在一個商品選擇界面,選擇好商品,并且進行編輯/刪除/更新后,   最后一并交給數(shù)據(jù)庫,而不是每一步操作都訪問數(shù)據(jù)庫,因為客戶選擇商品可能進行n次編輯/刪除   更新操作,如果每次都提交,不但容易引起數(shù)據(jù)庫沖突,引發(fā)錯誤,而且當數(shù)據(jù)量很大時在用戶執(zhí)行   效率上也變得有些慢   b.有的界面是這樣的有的界面是這樣的,需求要求一定用緩存實現(xiàn),確認之前的操作不提交到庫,點擊   頁面專門提交的按鈕時才提交商品選擇信息和商品的其它信息. 我經(jīng)常遇到這樣的情況   c.有些情況下只往數(shù)據(jù)庫里更新,不讀取. 也就是說沒有從數(shù)據(jù)庫里讀,SqlDataAdapter也就不知道是   更新哪張表了,調(diào)用Update就很可能出錯了。這樣的情況下可以用SqlCommandBuilder 了   (此段參考了他人所作)   d.在使用adapter的時候如果不是用設計器的時候,并且要用到adapter.Update()函數(shù)的時候,這時候要注意   SqlCommandBuilder必須要有   常見錯誤:   一些初學者經(jīng)常會在使用adapter的時候忘了使用SqlCommandBuilder,即使用了也會忘了用   myAdapter.UpdateCommand = myCommandBuilder .GetUpdateCommand();//更新,   還自以為是很對,感覺煩躁不可救藥。   摘自msdn   The following example uses the derived class, OleDbDataAdapter, to update the data source.   C#   public DataSet CreateCmdsAndUpdate(string connectionString, string queryString)   {   using (OleDbConnection connection = new OleDbConnection(connectionString))   {   OleDbDataAdapter adapter = new OleDbDataAdapter();   adapter.SelectCommand = new OleDbCommand(queryString, connection);   OleDbCommandBuilder builder = new OleDbCommandBuilder(adapter);   connection.Open();   DataSet customers = new DataSet();   adapter.Fill(customers);   //code to modify data in dataset here   adapter.Update(customers);   return customers;   }   }

轉(zhuǎn)載于:https://www.cnblogs.com/rjxiaozhou/archive/2010/06/25/1765178.html

總結(jié)

以上是生活随笔為你收集整理的SqlCommandBuilder的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。