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

創(chuàng)新互聯(lián)建站服務(wù)項目包括福鼎網(wǎng)站建設(shè)、福鼎網(wǎng)站制作、福鼎網(wǎng)頁制作以及福鼎網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢、行業(yè)經(jīng)驗、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,福鼎網(wǎng)站推廣取得了明顯的社會效益與經(jīng)濟效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到福鼎省份的部分城市,未來相信會繼續(xù)擴大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!
在用Visual C#刪除記錄的時候要注意的是:必須從二個方面徹底刪除記錄,即從數(shù)據(jù)庫和用Visual C#編程時產(chǎn)生的一個DataSet對象中徹底刪除。在程序設(shè)計的時候,如果只是刪除了DataSet對象中的記錄信息,這種刪除是一種偽刪除。這是因為當(dāng)他退出程序,又重新運行程序,會發(fā)現(xiàn),那個要刪除的記錄依然還存在。這是因為DataSet對象只是對數(shù)據(jù)表的一個鏡像,并不是真正的記錄本身。但如果只是從數(shù)據(jù)庫中刪除記錄,因為我們此時程序用到的數(shù)據(jù)集合是從DataSet對象中讀取的,子DataSet對象中依然保存此條記錄的鏡像。所以就會發(fā)現(xiàn),我們根本沒有刪除掉記錄,但實際上已經(jīng)刪除了。此時只有退出程序,重新運行,才會發(fā)現(xiàn)記錄已經(jīng)刪除了。本文使用的方法是刪除以上二個方面的記錄或記錄鏡像信息。當(dāng)然你也可以使用其他的方法,譬如:首先從數(shù)據(jù)庫中刪除記錄,然后重新建立數(shù)據(jù)連接,重新創(chuàng)建一個新的DataSet對象。這種方法雖然也可以達到相同目的,但顯然相對繁雜些,所以本文采用的是***種方法--直接刪除。在程序中具體的實現(xiàn)語句如下:
- //連接到一個數(shù)據(jù)庫
- 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ù)庫中刪除指定記錄
- 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è)計中大致差不多,具體的實現(xiàn)方式也是通過SQL語句調(diào)用來實現(xiàn)的。下面就是在程序中修改記錄的具體語句:
- //連接到一個數(shù)據(jù)庫
- string strCon = " Provider =
- Microsoft.Jet.OLEDB.4.0 ; Data Source =
- sample.mdb " ;
- OleDbConnection myConn =
- new OleDbConnection ( strCon ) ;
- myConn.Open ( ) ;
- //從數(shù)據(jù)庫中修改指定記錄
- 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ù)庫記錄》文的內(nèi)容,就可以得到用Visual C#完成刪除和修改數(shù)據(jù)記錄的比較完整程序代碼。
Visual C#數(shù)據(jù)表操作:用Visual C#實現(xiàn)刪除和修改數(shù)據(jù)庫記錄的完整源程序代碼
- 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 ( ) {
- // 對窗體中所需要的內(nèi)容進行初始化
- InitializeComponent ( ) ;
- //連接到一個數(shù)據(jù)庫
- GetConnected ( ) ;
- }
- //清除程序中用到的所有資源
- public override void Dispose ( ) {
- base.Dispose ( ) ;
- components.Dispose ( ) ;
- }
- public void GetConnected ( )
- {
- try{
- //創(chuàng)建一個 OleDbConnection對象
- string strCon = " Provider =
- Microsoft.Jet.OLEDB.4.0 ; Data Source =
- sample.mdb " ;
- OleDbConnection myConn =
- new OleDbConnection ( strCon ) ;
- string strCom = " SELECT * FROM books " ;
- //創(chuàng)建一個 DataSet對象
- 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ù)表的某
- 個字段綁定在不同的綁定到文本框"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
- //把對象DataSet和"books"數(shù)據(jù)表綁定到此myBind對象
- myBind = this.BindingContext [ myDataSet , "books" ] ;
- isBound = true ;
- }
- }
- catch ( Exception e )
- {
- MessageBox.Show ( "連接數(shù)據(jù)庫發(fā)生錯誤為:"
- + 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 ) ;
【編輯推薦】
- 詳細介紹C#編譯器
- C#異常機制的相關(guān)解釋
- 在C#程序編譯另一個程序的實現(xiàn)方法
- C#類庫編譯兩步走
- C#條件編譯指令淺析
網(wǎng)站題目:Visual C#數(shù)據(jù)表操作之刪除和修改記錄
網(wǎng)站URL:http://m.fisionsoft.com.cn/article/djpigoc.html


咨詢
建站咨詢
