用Visual C#來修改和刪除資料庫記錄

來源:互聯網
上載者:User
visual|資料|資料庫 一.程式設計和啟動並執行環境設定:
(1).視窗2000伺服器版
(2).Microsoft Access Data Component 2.6 以上版本 ( MADC 2.6 )
(3).本文程式使用的資料庫的介紹:

為了方便起見,在選用資料庫方面選用了本機資料庫Access 2000,當然你也可以選用其他類型的資料庫,只需要更改文章後面的程式原始碼中資料庫的引擎,並更改對應的代碼就可以了。本程式中使用的資料庫名稱為sample.mdb,在此資料庫中有一張資料表books。此資料表的結構如下:
欄位名稱欄位類型代表意思Bookid數字序號booktitle文本書籍名稱bookauthor文本書籍作者bookprice數字價格bookstock數字書架號
二.程式設計痛點和應該注意的問題:
在程式設計中的重點和痛點就是如何用Visual C#刪除記錄和如何修改記錄。下面就這二個問題進行必要的論述:
(1).如何用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 ( ) ;

(2).用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#實現刪除和修改資料庫記錄的完整來源程式代碼:
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 ) ;
t_bookstock.Size = new System.Drawing.Size ( 80 , 20 ) ;
//以下是設定在程式中使用到的Label屬性
l_bookid.Location = new System.Drawing.Point ( 24 , 56 ) ;
l_bookid.Text = "序 號:" ;
l_bookid.Size = new System.Drawing.Size ( 142 , 20 ) ;
l_bookid.Font = new System.Drawing.Font ( "宋體" , 12f ) ;
l_bookid.TextAlign = System.Drawing.ContentAlignment.MiddleCenter ;
l_booktitle.Location = new System.Drawing.Point ( 24 , 108 ) ;
l_booktitle.Text = "書 名:" ;
l_booktitle.Size = new System.Drawing.Size ( 142 , 20 ) ;
l_booktitle.Font = new System.Drawing.Font ( "宋體" , 12f ) ;
l_booktitle.TextAlign = System.Drawing.ContentAlignment.MiddleCenter ;
l_bookauthor.Location = new System.Drawing.Point ( 24 , 160 ) ;
l_bookauthor.Text = "作 者:";
l_bookauthor.Size = new System.Drawing.Size ( 142 , 20 ) ;
l_bookauthor.Font = new System.Drawing.Font ( "宋體" , 12f ) ;
l_bookauthor.TextAlign = System.Drawing.ContentAlignment.MiddleCenter ;
l_bookprice.Location = new System.Drawing.Point ( 24 , 212 ) ;
l_bookprice.Text = "價 格:" ;
l_bookprice.Size = new System.Drawing.Size ( 142 , 20 ) ;
l_bookprice.Font = new System.Drawing.Font ( "宋體" , 12f ) ;
l_bookprice.TextAlign = System.Drawing.ContentAlignment.MiddleCenter ;

l_bookstock.Location = new System.Drawing.Point ( 24 , 264 ) ;
l_bookstock.Text = "書 架 號:" ;
l_bookstock.Size = new System.Drawing.Size ( 142 , 20 ) ;
l_bookstock.Font = new System.Drawing.Font ( "宋體" , 12f ) ;
l_bookstock.TextAlign = System.Drawing.ContentAlignment.MiddleCenter ;

//以下設定程式中用到的功能按鈕的屬性及對應的事件
delete.Location = new System.Drawing.Point ( 104 , 352 ) ;
delete.ForeColor = System.Drawing.Color.Black ;
delete.Size = new System.Drawing.Size ( 80 , 24 ) ;
delete.Font = new System.Drawing.Font ( "宋體" , 9f ) ;
delete.Text = "刪除記錄" ;
delete.Click += new System.EventHandler ( GoDelete ) ;

update.Location = new System.Drawing.Point ( 204 , 352 ) ;
update.ForeColor = System.Drawing.Color.Black ;
update.Size = new System.Drawing.Size ( 80 , 24 ) ;
update.Font = new System.Drawing.Font ( "宋體" , 9f ) ;
update.Text = "修改記錄" ;
update.Click += new System.EventHandler ( GoUpdate ) ;

firstrec.Location = new System.Drawing.Point ( 64 , 312 ) ;
firstrec.ForeColor = System.Drawing.Color.Black ;
firstrec.Size = new System.Drawing.Size ( 40 , 24 ) ;
firstrec.Font = new System.Drawing.Font ( "宋體" , 9f ) ;
firstrec.Text = "首記錄" ;
firstrec.Click += new System.EventHandler ( GoFirst ) ;

previousrec.Location = new System.Drawing.Point ( 136 , 312 ) ;
previousrec.ForeColor = System.Drawing.Color.Black ;
previousrec.Size = new System.Drawing.Size ( 40 , 24 ) ;
previousrec.Font = new System.Drawing.Font ( "宋體" , 9f ) ;
previousrec.Text = "上一條" ;
previousrec.Click += new System.EventHandler ( GoPrevious ) ;

nextrec.Location = new System.Drawing.Point ( 208 , 312 ) ;
nextrec.ForeColor = System.Drawing.Color.Black ;
nextrec.Size = new System.Drawing.Size ( 40 , 24 ) ;
nextrec.Font = new System.Drawing.Font ( "宋體" , 9f ) ;
nextrec.Text = "下一條" ;
nextrec.Click += new System.EventHandler ( GoNext ) ;

lastrec.Location = new System.Drawing.Point ( 280 , 312 ) ;
lastrec.ForeColor = System.Drawing.Color.Black ;
lastrec.Size = new System.Drawing.Size ( 40 , 24 ) ;
lastrec.Font = new System.Drawing.Font ( "宋體" , 9f ) ;
lastrec.Text = "尾記錄" ;
lastrec.Click += new System.EventHandler ( GoLast ) ;

label1.Location = new System.Drawing.Point ( 60 , 20 ) ;
label1.Text = "用Visual C#來修改和刪除資料庫中的記錄" ;
label1.Size = new System.Drawing.Size ( 296 , 24 ) ;
label1.ForeColor = System.Drawing.SystemColors.Desktop ;
label1.Font = new System.Drawing.Font ( "宋體" , 14f ) ;
//設定程式的主表單的屬性
this.Text = "用Visual C#來修改和刪除資料庫中的記錄!" ;
this.AutoScaleBaseSize = new System.Drawing.Size ( 5 , 13 ) ;
this.FormBorderStyle = FormBorderStyle.FixedSingle ;
this.ClientSize = new System.Drawing.Size ( 394 , 425 ) ;
//在主表單中加入組件
this.Controls.Add ( delete ) ;
this.Controls.Add ( update ) ;
this.Controls.Add ( lastrec ) ;
this.Controls.Add ( nextrec ) ;
this.Controls.Add ( previousrec ) ;
this.Controls.Add ( firstrec ) ;
this.Controls.Add ( t_bookstock ) ;
this.Controls.Add ( t_bookprice ) ;
this.Controls.Add ( t_bookauthor ) ;
this.Controls.Add ( t_booktitle ) ;
this.Controls.Add ( t_bookid ) ;
this.Controls.Add ( l_bookstock ) ;
this.Controls.Add ( l_bookprice ) ;
this.Controls.Add ( l_bookauthor ) ;
this.Controls.Add ( l_booktitle ) ;
this.Controls.Add ( l_bookid ) ;
this.Controls.Add ( label1 ) ;

}
//"刪除記錄"對應的事件
protected void GoDelete ( object sender, System.EventArgs e )
{
try{
//串連到一個資料庫
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 ( ) ;
}
catch ( Exception ed )
{
MessageBox.Show ( "刪除記錄錯誤資訊: " + ed.ToString ( ) , "錯誤!" ) ;
}
}
//"修改記錄"按鈕對應的事件
protected void GoUpdate ( object sender , System.EventArgs e )
{
int i = myBind.Position ;
try{
//串連到一個資料庫
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 ( ) ;
}
catch ( Exception ed )
{
MessageBox.Show ( "修改指定記錄錯誤: " + ed.ToString ( ) , "錯誤!" ) ;
}
myBind.Position = i ;
}
//"尾記錄"按鈕對應的事件
protected void GoLast ( object sender , System.EventArgs e )
{
myBind.Position = myBind.Count - 1 ;
}
//"下一條"按鈕對應的事件
protected void GoNext ( object sender , System.EventArgs e )
{
if ( myBind.Position == myBind.Count - 1 )
MessageBox.Show ( "已經到尾記錄!" ) ;
else
myBind.Position += 1 ;
}
//"上一條"按鈕對應的事件
protected void GoPrevious ( object sender , System.EventArgs e )
{
if ( myBind.Position == 0 )
MessageBox.Show ( "已經到首記錄!" ) ;
else
myBind.Position -= 1 ;
}
//"首記錄"按鈕對應的事件
protected void GoFirst ( object sender , System.EventArgs e )
{
myBind.Position = 0 ;
}
}

四.編譯來源程式代碼,產生執行檔案:
得到了Data.cs來源程式代碼以後,經過下面編譯命令編譯成功後,即可得到執行檔案data.exe:
csc /t:winexe /r:system.windows.forms.dll /r:system.data.dll data.cs
五.總結:
本文主要介紹了如何用Visual C#來刪除和修改記錄。以及在處理這些操作的時候,應該注意的一些重要問題及處理的方法。如果你的機器已經滿足本文要求的運行環境,那麼在產生的執行檔案目錄中建立一個sample.mdb資料庫,在資料庫中建立一個books資料表,資料表的結構可按照本文上面提供的結構來建立,在這一切都完畢後,就可以運行程式,享受Visual C#給我們帶來的資料操作的快感了。


相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。