Visual C#数据表操作之删除和修改记录

开发 后端
本文使用Visual C#正确删除数据表中的记录 、修改数据表中的记录等实现删除和修改数据库记录方法。文章提供了Visual C#数据表操作的实现代码。

Visual C#数据表操作:用Visual C#正确删除数据表中的记录

在用Visual C#删除记录的时候要注意的是:必须从二个方面彻底删除记录,即从数据库和用Visual C#编程时产生的一个DataSet对象中彻底删除。在程序设计的时候,如果只是删除了DataSet对象中的记录信息,这种删除是一种伪删除。这是因为当他退出程序,又重新运行程序,会发现,那个要删除的记录依然还存在。这是因为DataSet对象只是对数据表的一个镜像,并不是真正的记录本身。但如果只是从数据库中删除记录,因为我们此时程序用到的数据集合是从DataSet对象中读取的,子DataSet对象中依然保存此条记录的镜像。所以就会发现,我们根本没有删除掉记录,但实际上已经删除了。此时只有退出程序,重新运行,才会发现记录已经删除了。本文使用的方法是删除以上二个方面的记录或记录镜像信息。当然你也可以使用其他的方法,譬如:首先从数据库中删除记录,然后重新建立数据连接,重新创建一个新的DataSet对象。这种方法虽然也可以达到相同目的,但显然相对繁杂些,所以本文采用的是***种方法--直接删除。在程序中具体的实现语句如下:

  1. //连接到一个数据库   
  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. //从数据库中删除指定记录   
  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#数据表操作:用Visual C#来修改数据表中的记录

在用Visual C#修改记录和删除记录,在程序设计中大致差不多,具体的实现方式也是通过SQL语句调用来实现的。下面就是在程序中修改记录的具体语句:

  1. //连接到一个数据库   
  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. //从数据库中修改指定记录   
  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#删除和修改记录以后,结合《Visual C#中轻松浏览数据库记录》文的内容,就可以得到用Visual C#完成删除和修改数据记录的比较完整程序代码。

Visual C#数据表操作:用Visual C#实现删除和修改数据库记录的完整源程序代码

  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. //定义此变量,是判断组件是否已经绑定数据表中的字段   
  58.  
  59. public DataEdit ( ) {   
  60.  
  61. // 对窗体中所需要的内容进行初始化   
  62.  
  63. InitializeComponent ( ) ;   
  64.  
  65. //连接到一个数据库   
  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. //创建一个 OleDbConnection对象   
  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. //创建一个 DataSet对象   
  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. //判断数据字段是否绑定到 TextBoxes   
  112.  
  113. if ( !isBound )   
  114.  
  115. {   
  116.  
  117. //以下是为显示数据记录而把数据表的某  
  118. 个字段绑定在不同的绑定到文本框"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. //设定 BindingManagerBase   
  136.  
  137. //把对象DataSet和"books"数据表绑定到此myBind对象   
  138.  
  139. myBind = this.BindingContext [ myDataSet , "books" ] ;   
  140.  
  141. isBound = true ;   
  142. }   
  143. }   
  144. catch ( Exception e )   
  145. {   
  146. MessageBox.Show ( "连接数据库发生错误为:"   
  147. + e.ToString ( ) , "错误!" ) ;   
  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. 详细介绍C#编译器
  2. C#异常机制的相关解释
  3. 在C#程序编译另一个程序的实现方法
  4. C#类库编译两步走
  5. C#条件编译指令浅析
责任编辑:冰荷 来源: xsrss
相关推荐

2009-08-19 13:34:55

C#操作注册表

2009-08-12 18:35:17

C#数据结构

2009-11-11 17:02:01

ADO修改记录

2009-08-19 15:47:09

C#操作Access

2009-08-10 16:47:45

Visual C#数据

2009-08-19 13:30:58

C#操作注册表

2009-08-19 13:25:53

C#操作注册表

2009-08-19 16:50:32

Visual C#C#语言特性

2011-05-18 15:08:03

mysql删除修改数据

2022-05-20 08:18:24

Git存储哈希值

2009-08-19 13:38:06

C#操作注册表

2009-09-02 16:21:17

Visual BasiC#语言

2009-08-19 16:40:26

C#操作Access数

2009-05-25 15:42:03

Visual StudC#

2009-08-07 15:38:15

精通C#数据库编程

2009-07-02 09:40:17

JSP导出Oracle

2009-08-11 14:12:27

C# ListView

2010-11-24 10:52:57

Mysql字符集

2009-08-10 18:05:19

C#数据库查询

2009-08-20 10:25:37

C#操作内存
点赞
收藏

51CTO技术栈公众号