日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
Visual C#數(shù)據(jù)表操作之刪除和修改記錄

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

創(chuàng)新互聯(lián)建站服務(wù)項(xiàng)目包括福鼎網(wǎng)站建設(shè)、福鼎網(wǎng)站制作、福鼎網(wǎng)頁(yè)制作以及福鼎網(wǎng)絡(luò)營(yíng)銷(xiāo)策劃等。多年來(lái),我們專(zhuān)注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢(shì)、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,福鼎網(wǎng)站推廣取得了明顯的社會(huì)效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到福鼎省份的部分城市,未來(lái)相信會(huì)繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!

在用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),我們根本沒(méi)有刪除掉記錄,但實(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#來(lái)修改數(shù)據(jù)表中的記錄

在用Visual C#修改記錄和刪除記錄,在程序設(shè)計(jì)中大致差不多,具體的實(shí)現(xiàn)方式也是通過(guò)SQL語(yǔ)句調(diào)用來(lái)實(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#類(lèi)庫(kù)編譯兩步走
  5. C#條件編譯指令淺析

文章題目:Visual C#數(shù)據(jù)表操作之刪除和修改記錄
文章路徑:http://m.5511xx.com/article/djpigoc.html