這篇文章主要講解了“Visual C#數(shù)據(jù)表怎么刪除和修改記錄”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“Visual C#數(shù)據(jù)表怎么刪除和修改記錄”吧!
為宜城等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計制作服務(wù),及宜城網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為網(wǎng)站制作、網(wǎng)站設(shè)計、宜城網(wǎng)站設(shè)計,以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會得到認(rèn)可,從而選擇與我們長期合作。這樣,我們也可以走得更遠(yuǎn)!
Visual C#數(shù)據(jù)表操作:用Visual C#正確刪除數(shù)據(jù)表中的記錄
在用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對象。這種方法雖然也可以達(dá)到相同目的,但顯然相對繁雜些,所以本文采用的是***種方法--直接刪除。在程序中具體的實現(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)容進(jìn)行初始化 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 ) ;
感謝各位的閱讀,以上就是“Visual C#數(shù)據(jù)表怎么刪除和修改記錄”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對Visual C#數(shù)據(jù)表怎么刪除和修改記錄這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!