VisualC#數(shù)據(jù)表怎么刪除和修改記錄

這篇文章主要講解了“Visual C#數(shù)據(jù)表怎么刪除和修改記錄”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“Visual C#數(shù)據(jù)表怎么刪除和修改記錄”吧!

為宜城等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計(jì)制作服務(wù),及宜城網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為網(wǎng)站制作、網(wǎng)站設(shè)計(jì)、宜城網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長期合作。這樣,我們也可以走得更遠(yuǎn)!

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

在用Visual C#刪除記錄的時(shí)候要注意的是:必須從二個(gè)方面徹底刪除記錄,即從數(shù)據(jù)庫和用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ù)庫中刪除記錄,因?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ù)庫中刪除記錄,然后重新建立數(shù)據(jù)連接,重新創(chuàng)建一個(gè)新的DataSet對(duì)象。這種方法雖然也可以達(dá)到相同目的,但顯然相對(duì)繁雜些,所以本文采用的是***種方法--直接刪除。在程序中具體的實(shí)現(xiàn)語句如下:

//連接到一個(gè)數(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è)計(jì)中大致差不多,具體的實(shí)現(xiàn)方式也是通過SQL語句調(diào)用來實(shí)現(xiàn)的。下面就是在程序中修改記錄的具體語句:

//連接到一個(gè)數(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#實(shí)現(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 ( ) {    // 對(duì)窗體中所需要的內(nèi)容進(jìn)行初始化    InitializeComponent ( ) ;    //連接到一個(gè)數(shù)據(jù)庫    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ù)庫發(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 ) ;

感謝各位的閱讀,以上就是“Visual C#數(shù)據(jù)表怎么刪除和修改記錄”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對(duì)Visual C#數(shù)據(jù)表怎么刪除和修改記錄這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!

分享標(biāo)題:VisualC#數(shù)據(jù)表怎么刪除和修改記錄
分享網(wǎng)址:http://muchs.cn/article20/ihggjo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供小程序開發(fā)、網(wǎng)站收錄、網(wǎng)站排名網(wǎng)站導(dǎo)航、靜態(tài)網(wǎng)站、網(wǎng)站改版

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

搜索引擎優(yōu)化