自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Visual C#數(shù)據(jù)表操作之刪除和修改記錄

開發(fā) 后端
本文使用Visual C#正確刪除數(shù)據(jù)表中的記錄 、修改數(shù)據(jù)表中的記錄等實(shí)現(xiàn)刪除和修改數(shù)據(jù)庫(kù)記錄方法。文章提供了Visual C#數(shù)據(jù)表操作的實(shí)現(xiàn)代碼。

Visual C#數(shù)據(jù)表操作:用Visual C#正確刪除數(shù)據(jù)表中的記錄

在用Visual C#刪除記錄的時(shí)候要注意的是:必須從二個(gè)方面徹底刪除記錄,即從數(shù)據(jù)庫(kù)和用Visual C#編程時(shí)產(chǎn)生的一個(gè)DataSet對(duì)象中徹底刪除。在程序設(shè)計(jì)的時(shí)候,如果只是刪除了DataSet對(duì)象中的記錄信息,這種刪除是一種偽刪除。這是因?yàn)楫?dāng)他退出程序,又重新運(yùn)行程序,會(huì)發(fā)現(xiàn),那個(gè)要?jiǎng)h除的記錄依然還存在。這是因?yàn)镈ataSet對(duì)象只是對(duì)數(shù)據(jù)表的一個(gè)鏡像,并不是真正的記錄本身。但如果只是從數(shù)據(jù)庫(kù)中刪除記錄,因?yàn)槲覀兇藭r(shí)程序用到的數(shù)據(jù)集合是從DataSet對(duì)象中讀取的,子DataSet對(duì)象中依然保存此條記錄的鏡像。所以就會(huì)發(fā)現(xiàn),我們根本沒有刪除掉記錄,但實(shí)際上已經(jīng)刪除了。此時(shí)只有退出程序,重新運(yùn)行,才會(huì)發(fā)現(xiàn)記錄已經(jīng)刪除了。本文使用的方法是刪除以上二個(gè)方面的記錄或記錄鏡像信息。當(dāng)然你也可以使用其他的方法,譬如:首先從數(shù)據(jù)庫(kù)中刪除記錄,然后重新建立數(shù)據(jù)連接,重新創(chuàng)建一個(gè)新的DataSet對(duì)象。這種方法雖然也可以達(dá)到相同目的,但顯然相對(duì)繁雜些,所以本文采用的是***種方法--直接刪除。在程序中具體的實(shí)現(xiàn)語(yǔ)句如下:

  1. //連接到一個(gè)數(shù)據(jù)庫(kù)   
  2.  
  3. string strCon = " Provider =   
  4. Microsoft.Jet.OLEDB.4.0 ; Data Source =   
  5. sample.mdb " ;   
  6.  
  7. OleDbConnection myConn =   
  8. new OleDbConnection ( strCon ) ;   
  9.  
  10. myConn.Open ( ) ;   
  11.  
  12. string strDele = "  
  13. DELETE FROM books WHERE bookid= "  
  14.  + t_bookid.Text ;   
  15.  
  16. OleDbCommand myCommand =   
  17. new OleDbCommand ( strDele , myConn ) ;   
  18.  
  19. //從數(shù)據(jù)庫(kù)中刪除指定記錄   
  20.  
  21. myCommand.ExecuteNonQuery ( ) ;   
  22.  
  23. //從DataSet中刪除指定記錄信息   
  24.  
  25. myDataSet.Tables [ "books" ] . Rows   
  26. [ myBind.Position ] . Delete ( ) ;   
  27.  
  28. myDataSet.Tables [ "books" ] .   
  29. AcceptChanges ( ) ;   
  30.  
  31. myConn.Close ( ) ;   

Visual C#數(shù)據(jù)表操作:用Visual C#來修改數(shù)據(jù)表中的記錄

在用Visual C#修改記錄和刪除記錄,在程序設(shè)計(jì)中大致差不多,具體的實(shí)現(xiàn)方式也是通過SQL語(yǔ)句調(diào)用來實(shí)現(xiàn)的。下面就是在程序中修改記錄的具體語(yǔ)句:

  1. //連接到一個(gè)數(shù)據(jù)庫(kù)   
  2.  
  3. string strCon = " Provider =   
  4. Microsoft.Jet.OLEDB.4.0 ; Data Source =   
  5. sample.mdb " ;   
  6.  
  7. OleDbConnection myConn =   
  8. new OleDbConnection ( strCon ) ;   
  9.  
  10. myConn.Open ( ) ;   
  11.  
  12. //從數(shù)據(jù)庫(kù)中修改指定記錄   
  13.  
  14. string strUpdt = "   
  15. UPDATE books SET booktitle = '"   
  16.  
  17. + t_booktitle.Text + "' , bookauthor = '"   
  18.  
  19. + t_bookauthor.Text + "' , bookprice = "   
  20.  
  21. + t_bookprice.Text + " , bookstock = "   
  22.  
  23. + t_bookstock.Text + " WHERE bookid = " 
  24.  + t_bookid.Text ;   
  25.  
  26. OleDbCommand myCommand =   
  27. new OleDbCommand ( strUpdt , myConn ) ;   
  28.  
  29. myCommand.ExecuteNonQuery ( ) ;   
  30.  
  31. myConn.Close ( ) ;   

(3).在了解了如何用Visual C#刪除和修改記錄以后,結(jié)合《Visual C#中輕松瀏覽數(shù)據(jù)庫(kù)記錄》文的內(nèi)容,就可以得到用Visual C#完成刪除和修改數(shù)據(jù)記錄的比較完整程序代碼。

Visual C#數(shù)據(jù)表操作:用Visual C#實(shí)現(xiàn)刪除和修改數(shù)據(jù)庫(kù)記錄的完整源程序代碼

  1. using System ;   
  2.  
  3. using System.Drawing ;   
  4.  
  5. using System.ComponentModel ;   
  6.  
  7. using System.Windows.Forms ;   
  8.  
  9. using System.Data.OleDb ;   
  10.  
  11. using System.Data ;   
  12.  
  13. public class DataEdit :  
  14.  Form { private System.ComponentModel.  
  15. Container components ;   
  16.  
  17. private Button delete ;   
  18.  
  19. private Button update ;   
  20.  
  21. private Button lastrec ;   
  22.  
  23. private Button nextrec ;   
  24.  
  25. private Button previousrec ;   
  26.  
  27. private Button firstrec ;   
  28.  
  29. private TextBox t_bookstock ;   
  30.  
  31. private TextBox t_bookprice ;   
  32.  
  33. private TextBox t_bookauthor ;   
  34.  
  35. private TextBox t_booktitle ;   
  36.  
  37. private TextBox t_bookid ;   
  38.  
  39. private Label l_bookstock ;   
  40.  
  41. private Label l_bookprice ;   
  42.  
  43. private Label l_bookauthor ;   
  44.  
  45. private Label l_booktitle ;   
  46.  
  47. private Label l_bookid ;   
  48.  
  49. private Label label1 ;   
  50.  
  51. private System.Data.DataSet myDataSet ;   
  52.  
  53. private BindingManagerBase myBind ;   
  54.  
  55. private bool isBound = false ;   
  56.  
  57. //定義此變量,是判斷組件是否已經(jīng)綁定數(shù)據(jù)表中的字段   
  58.  
  59. public DataEdit ( ) {   
  60.  
  61. // 對(duì)窗體中所需要的內(nèi)容進(jìn)行初始化   
  62.  
  63. InitializeComponent ( ) ;   
  64.  
  65. //連接到一個(gè)數(shù)據(jù)庫(kù)   
  66.  
  67. GetConnected ( ) ;   
  68.  
  69. }   
  70.  
  71. //清除程序中用到的所有資源   
  72.  
  73. public override void Dispose ( ) {   
  74.  
  75. base.Dispose ( ) ;   
  76.  
  77. components.Dispose ( ) ;   
  78.  
  79. }   
  80.  
  81. public void GetConnected ( )   
  82.  
  83. {   
  84.  
  85. try{   
  86.  
  87. //創(chuàng)建一個(gè) OleDbConnection對(duì)象   
  88.  
  89. string strCon = " Provider =   
  90. Microsoft.Jet.OLEDB.4.0 ; Data Source =   
  91. sample.mdb " ;   
  92.  
  93. OleDbConnection myConn =   
  94. new OleDbConnection ( strCon ) ;   
  95.  
  96. string strCom = " SELECT * FROM books " ;   
  97.  
  98. //創(chuàng)建一個(gè) DataSet對(duì)象   
  99.  
  100. myDataSet = new DataSet ( ) ;   
  101.  
  102. myConn.Open ( ) ;   
  103.  
  104. OleDbDataAdapter myCommand =   
  105. new OleDbDataAdapter ( strCom , myConn ) ;   
  106.  
  107. myCommand.Fill ( myDataSet , "books" ) ;   
  108.  
  109. myConn.Close ( ) ;   
  110.  
  111. //判斷數(shù)據(jù)字段是否綁定到 TextBoxes   
  112.  
  113. if ( !isBound )   
  114.  
  115. {   
  116.  
  117. //以下是為顯示數(shù)據(jù)記錄而把數(shù)據(jù)表的某  
  118. 個(gè)字段綁定在不同的綁定到文本框"Text"屬性上   
  119.  
  120. t_bookid.DataBindings.Add ( "  
  121. Text" , myDataSet , "books.bookid" ) ;   
  122.  
  123. t_booktitle.DataBindings.Add ( "  
  124. Text" , myDataSet , "books.booktitle" ) ;   
  125.  
  126. t_bookauthor.DataBindings.Add ( "  
  127. Text" , myDataSet , "books.bookauthor" ) ;   
  128.  
  129. t_bookprice.DataBindings.Add ( "  
  130. Text" , myDataSet , "books.bookprice" ) ;   
  131.  
  132. t_bookstock.DataBindings.Add ( "  
  133. Text" , myDataSet , "books.bookstock" ) ;   
  134.  
  135. //設(shè)定 BindingManagerBase   
  136.  
  137. //把對(duì)象DataSet和"books"數(shù)據(jù)表綁定到此myBind對(duì)象   
  138.  
  139. myBind = this.BindingContext [ myDataSet , "books" ] ;   
  140.  
  141. isBound = true ;   
  142. }   
  143. }   
  144. catch ( Exception e )   
  145. {   
  146. MessageBox.Show ( "連接數(shù)據(jù)庫(kù)發(fā)生錯(cuò)誤為:"   
  147. + e.ToString ( ) , "錯(cuò)誤!" ) ;   
  148. }   
  149. }   
  150. public static void Main ( ) {   
  151. Application.Run ( new DataEdit ( ) ) ;   
  152. }   
  153. private void InitializeComponent ( )   
  154. {   
  155.  
  156. this.components =   
  157. new System.ComponentModel.Container ( ) ;   
  158.  
  159. this.t_bookid = new TextBox ( ) ;   
  160.  
  161. this.previousrec = new Button ( ) ;   
  162.  
  163. this.l_bookauthor = new Label ( ) ;   
  164.  
  165. this.delete = new Button ( ) ;   
  166.  
  167. this.t_booktitle = new TextBox ( ) ;   
  168.  
  169. this.t_bookauthor = new TextBox ( ) ;   
  170.  
  171. this.t_bookprice = new TextBox ( ) ;   
  172.  
  173. this.l_bookprice = new Label ( ) ;   
  174.  
  175. this.t_bookstock = new TextBox ( ) ;   
  176.  
  177. this.l_bookstock = new Label ( ) ;   
  178.  
  179. this.l_booktitle = new Label ( ) ;   
  180.  
  181. this.update = new Button ( ) ;   
  182.  
  183. this.nextrec = new Button ( ) ;   
  184.  
  185. this.lastrec = new Button ( ) ;   
  186.  
  187. this.firstrec = new Button ( ) ;   
  188.  
  189. this.label1 = new Label ( ) ;   
  190.  
  191. this.l_bookid = new Label ( ) ;   
  192.  
  193. t_bookid.Location =   
  194. new System.Drawing.Point ( 184 , 56 ) ;   
  195.  
  196. t_bookid.Size =   
  197. new System.Drawing.Size ( 80 , 20 ) ;   
  198.  
  199. t_booktitle.Location =   
  200. new System.Drawing.Point ( 184 , 108 ) ;   
  201.  
  202. t_booktitle.Size =   
  203. new System.Drawing.Size ( 176 , 20 ) ;   
  204.  
  205. t_bookauthor.Location =   
  206. new System.Drawing.Point ( 184 , 160 ) ;   
  207.  
  208. t_bookauthor.Size =   
  209. new System.Drawing.Size ( 128 , 20 ) ;   
  210.  
  211. t_bookprice.Location =   
  212. new System.Drawing.Point ( 184 , 212 ) ;   
  213.  
  214. t_bookprice.Size =   
  215. new System.Drawing.Size ( 80 , 20 ) ;   
  216.  
  217. t_bookstock.Location =   
  218. new System.Drawing.Point ( 184 , 264 ) ;   

【編輯推薦】

  1. 詳細(xì)介紹C#編譯器
  2. C#異常機(jī)制的相關(guān)解釋
  3. 在C#程序編譯另一個(gè)程序的實(shí)現(xiàn)方法
  4. C#類庫(kù)編譯兩步走
  5. C#條件編譯指令淺析
責(zé)任編輯:冰荷 來源: xsrss
相關(guān)推薦

2009-08-19 13:34:55

C#操作注冊(cè)表

2009-08-12 18:35:17

C#數(shù)據(jù)結(jié)構(gòu)

2009-11-11 17:02:01

ADO修改記錄

2009-08-19 15:47:09

C#操作Access

2009-08-10 16:47:45

Visual C#數(shù)據(jù)

2009-08-19 13:30:58

C#操作注冊(cè)表

2009-08-19 13:25:53

C#操作注冊(cè)表

2009-08-19 16:50:32

Visual C#C#語(yǔ)言特性

2009-08-19 13:38:06

C#操作注冊(cè)表

2011-05-18 15:08:03

mysql刪除修改數(shù)據(jù)

2022-05-20 08:18:24

Git存儲(chǔ)哈希值

2009-08-19 16:40:26

C#操作Access數(shù)

2009-09-02 16:21:17

Visual BasiC#語(yǔ)言

2009-05-25 15:42:03

Visual StudC#

2009-08-11 14:12:27

C# ListView

2009-08-10 18:05:19

C#數(shù)據(jù)庫(kù)查詢

2009-08-20 10:25:37

C#操作內(nèi)存

2009-08-18 16:20:09

C# 操作Excel

2009-08-18 16:14:05

C# 操作Excel

2009-08-07 15:38:15

精通C#數(shù)據(jù)庫(kù)編程
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)