Visual C#數(shù)據(jù)表操作之刪除和修改記錄
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ǔ)句如下:
- //連接到一個(gè)數(shù)據(jù)庫(kù)
- 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 ) ;
- //從數(shù)據(jù)庫(kù)中刪除指定記錄
- myCommand.ExecuteNonQuery ( ) ;
- //從DataSet中刪除指定記錄信息
- myDataSet.Tables [ "books" ] . Rows
- [ myBind.Position ] . Delete ( ) ;
- myDataSet.Tables [ "books" ] .
- AcceptChanges ( ) ;
- 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ǔ)句:
- //連接到一個(gè)數(shù)據(jù)庫(kù)
- string strCon = " Provider =
- Microsoft.Jet.OLEDB.4.0 ; Data Source =
- sample.mdb " ;
- OleDbConnection myConn =
- new OleDbConnection ( strCon ) ;
- myConn.Open ( ) ;
- //從數(shù)據(jù)庫(kù)中修改指定記錄
- 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#刪除和修改記錄以后,結(jié)合《Visual C#中輕松瀏覽數(shù)據(jù)庫(kù)記錄》文的內(nèi)容,就可以得到用Visual C#完成刪除和修改數(shù)據(jù)記錄的比較完整程序代碼。
Visual C#數(shù)據(jù)表操作:用Visual C#實(shí)現(xiàn)刪除和修改數(shù)據(jù)庫(kù)記錄的完整源程序代碼
- 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 ;
- //定義此變量,是判斷組件是否已經(jīng)綁定數(shù)據(jù)表中的字段
- public DataEdit ( ) {
- // 對(duì)窗體中所需要的內(nèi)容進(jìn)行初始化
- InitializeComponent ( ) ;
- //連接到一個(gè)數(shù)據(jù)庫(kù)
- GetConnected ( ) ;
- }
- //清除程序中用到的所有資源
- public override void Dispose ( ) {
- base.Dispose ( ) ;
- components.Dispose ( ) ;
- }
- public void GetConnected ( )
- {
- try{
- //創(chuàng)建一個(gè) OleDbConnection對(duì)象
- string strCon = " Provider =
- Microsoft.Jet.OLEDB.4.0 ; Data Source =
- sample.mdb " ;
- OleDbConnection myConn =
- new OleDbConnection ( strCon ) ;
- string strCom = " SELECT * FROM books " ;
- //創(chuàng)建一個(gè) DataSet對(duì)象
- myDataSet = new DataSet ( ) ;
- myConn.Open ( ) ;
- OleDbDataAdapter myCommand =
- new OleDbDataAdapter ( strCom , myConn ) ;
- myCommand.Fill ( myDataSet , "books" ) ;
- myConn.Close ( ) ;
- //判斷數(shù)據(jù)字段是否綁定到 TextBoxes
- if ( !isBound )
- {
- //以下是為顯示數(shù)據(jù)記錄而把數(shù)據(jù)表的某
- 個(gè)字段綁定在不同的綁定到文本框"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" ) ;
- //設(shè)定 BindingManagerBase
- //把對(duì)象DataSet和"books"數(shù)據(jù)表綁定到此myBind對(duì)象
- myBind = this.BindingContext [ myDataSet , "books" ] ;
- isBound = true ;
- }
- }
- catch ( Exception e )
- {
- MessageBox.Show ( "連接數(shù)據(jù)庫(kù)發(fā)生錯(cuò)誤為:"
- + e.ToString ( ) , "錯(cuò)誤!" ) ;
- }
- }
- 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 ) ;
【編輯推薦】