Visual C#数据表操作:用Visual C#正确删除数据表中的记录
在用Visual C#删除记录的时候要注意的是:必须从二个方面彻底删除记录,即从数据库和用Visual C#编程时产生的一个DataSet对象中彻底删除。在程序设计的时候,如果只是删除了DataSet对象中的记录信息,这种删除是一种伪删除。这是因为当他退出程序,又重新运行程序,会发现,那个要删除的记录依然还存在。这是因为DataSet对象只是对数据表的一个镜像,并不是真正的记录本身。但如果只是从数据库中删除记录,因为我们此时程序用到的数据集合是从DataSet对象中读取的,子DataSet对象中依然保存此条记录的镜像。所以就会发现,我们根本没有删除掉记录,但实际上已经删除了。此时只有退出程序,重新运行,才会发现记录已经删除了。本文使用的方法是删除以上二个方面的记录或记录镜像信息。当然你也可以使用其他的方法,譬如:首先从数据库中删除记录,然后重新建立数据连接,重新创建一个新的DataSet对象。这种方法虽然也可以达到相同目的,但显然相对繁杂些,所以本文采用的是***种方法--直接删除。在程序中具体的实现语句如下:
- //连接到一个数据库
- string strCon = " Provider =
- Microsoft.Jet.OLEDB.4.0 ; Data Source =
- sample.mdb " ;
- OleDbConnection myConn =
- new OleDbConnection ( strCon ) ;
- myConn.Open ( ) ;
- string strDele = "
- DELETE FROM books WHERE bookid= "
- + t_bookid.Text ;
- OleDbCommand myCommand =
- new OleDbCommand ( strDele , myConn ) ;
- //从数据库中删除指定记录
- myCommand.ExecuteNonQuery ( ) ;
- //从DataSet中删除指定记录信息
- myDataSet.Tables [ "books" ] . Rows
- [ myBind.Position ] . Delete ( ) ;
- myDataSet.Tables [ "books" ] .
- AcceptChanges ( ) ;
- myConn.Close ( ) ;
Visual C#数据表操作:用Visual C#来修改数据表中的记录
在用Visual C#修改记录和删除记录,在程序设计中大致差不多,具体的实现方式也是通过SQL语句调用来实现的。下面就是在程序中修改记录的具体语句:
- //连接到一个数据库
- string strCon = " Provider =
- Microsoft.Jet.OLEDB.4.0 ; Data Source =
- sample.mdb " ;
- OleDbConnection myConn =
- new OleDbConnection ( strCon ) ;
- myConn.Open ( ) ;
- //从数据库中修改指定记录
- string strUpdt = "
- UPDATE books SET booktitle = '"
- + t_booktitle.Text + "' , bookauthor = '"
- + t_bookauthor.Text + "' , bookprice = "
- + t_bookprice.Text + " , bookstock = "
- + t_bookstock.Text + " WHERE bookid = "
- + t_bookid.Text ;
- OleDbCommand myCommand =
- new OleDbCommand ( strUpdt , myConn ) ;
- myCommand.ExecuteNonQuery ( ) ;
- myConn.Close ( ) ;
(3).在了解了如何用Visual C#删除和修改记录以后,结合《Visual C#中轻松浏览数据库记录》文的内容,就可以得到用Visual C#完成删除和修改数据记录的比较完整程序代码。
Visual C#数据表操作:用Visual C#实现删除和修改数据库记录的完整源程序代码
- using System ;
- using System.Drawing ;
- using System.ComponentModel ;
- using System.Windows.Forms ;
- using System.Data.OleDb ;
- using System.Data ;
- public class DataEdit :
- Form { private System.ComponentModel.
- Container components ;
- private Button delete ;
- private Button update ;
- private Button lastrec ;
- private Button nextrec ;
- private Button previousrec ;
- private Button firstrec ;
- private TextBox t_bookstock ;
- private TextBox t_bookprice ;
- private TextBox t_bookauthor ;
- private TextBox t_booktitle ;
- private TextBox t_bookid ;
- private Label l_bookstock ;
- private Label l_bookprice ;
- private Label l_bookauthor ;
- private Label l_booktitle ;
- private Label l_bookid ;
- private Label label1 ;
- private System.Data.DataSet myDataSet ;
- private BindingManagerBase myBind ;
- private bool isBound = false ;
- //定义此变量,是判断组件是否已经绑定数据表中的字段
- public DataEdit ( ) {
- // 对窗体中所需要的内容进行初始化
- InitializeComponent ( ) ;
- //连接到一个数据库
- GetConnected ( ) ;
- }
- //清除程序中用到的所有资源
- public override void Dispose ( ) {
- base.Dispose ( ) ;
- components.Dispose ( ) ;
- }
- public void GetConnected ( )
- {
- try{
- //创建一个 OleDbConnection对象
- string strCon = " Provider =
- Microsoft.Jet.OLEDB.4.0 ; Data Source =
- sample.mdb " ;
- OleDbConnection myConn =
- new OleDbConnection ( strCon ) ;
- string strCom = " SELECT * FROM books " ;
- //创建一个 DataSet对象
- myDataSet = new DataSet ( ) ;
- myConn.Open ( ) ;
- OleDbDataAdapter myCommand =
- new OleDbDataAdapter ( strCom , myConn ) ;
- myCommand.Fill ( myDataSet , "books" ) ;
- myConn.Close ( ) ;
- //判断数据字段是否绑定到 TextBoxes
- if ( !isBound )
- {
- //以下是为显示数据记录而把数据表的某
- 个字段绑定在不同的绑定到文本框"Text"属性上
- t_bookid.DataBindings.Add ( "
- Text" , myDataSet , "books.bookid" ) ;
- t_booktitle.DataBindings.Add ( "
- Text" , myDataSet , "books.booktitle" ) ;
- t_bookauthor.DataBindings.Add ( "
- Text" , myDataSet , "books.bookauthor" ) ;
- t_bookprice.DataBindings.Add ( "
- Text" , myDataSet , "books.bookprice" ) ;
- t_bookstock.DataBindings.Add ( "
- Text" , myDataSet , "books.bookstock" ) ;
- //设定 BindingManagerBase
- //把对象DataSet和"books"数据表绑定到此myBind对象
- myBind = this.BindingContext [ myDataSet , "books" ] ;
- isBound = true ;
- }
- }
- catch ( Exception e )
- {
- MessageBox.Show ( "连接数据库发生错误为:"
- + e.ToString ( ) , "错误!" ) ;
- }
- }
- public static void Main ( ) {
- Application.Run ( new DataEdit ( ) ) ;
- }
- private void InitializeComponent ( )
- {
- this.components =
- new System.ComponentModel.Container ( ) ;
- this.t_bookid = new TextBox ( ) ;
- this.previousrec = new Button ( ) ;
- this.l_bookauthor = new Label ( ) ;
- this.delete = new Button ( ) ;
- this.t_booktitle = new TextBox ( ) ;
- this.t_bookauthor = new TextBox ( ) ;
- this.t_bookprice = new TextBox ( ) ;
- this.l_bookprice = new Label ( ) ;
- this.t_bookstock = new TextBox ( ) ;
- this.l_bookstock = new Label ( ) ;
- this.l_booktitle = new Label ( ) ;
- this.update = new Button ( ) ;
- this.nextrec = new Button ( ) ;
- this.lastrec = new Button ( ) ;
- this.firstrec = new Button ( ) ;
- this.label1 = new Label ( ) ;
- this.l_bookid = new Label ( ) ;
- t_bookid.Location =
- new System.Drawing.Point ( 184 , 56 ) ;
- t_bookid.Size =
- new System.Drawing.Size ( 80 , 20 ) ;
- t_booktitle.Location =
- new System.Drawing.Point ( 184 , 108 ) ;
- t_booktitle.Size =
- new System.Drawing.Size ( 176 , 20 ) ;
- t_bookauthor.Location =
- new System.Drawing.Point ( 184 , 160 ) ;
- t_bookauthor.Size =
- new System.Drawing.Size ( 128 , 20 ) ;
- t_bookprice.Location =
- new System.Drawing.Point ( 184 , 212 ) ;
- t_bookprice.Size =
- new System.Drawing.Size ( 80 , 20 ) ;
- t_bookstock.Location =
- new System.Drawing.Point ( 184 , 264 ) ;
【编辑推荐】