用Visual C#來增加資料記錄(1)

來源:互聯網
上載者:User
visual|資料   在本篇文章中,我們將介紹Visual C#對資料庫的一個基本操作,即:如何往資料庫中添加記錄。我們將通過一些資料庫操作的例子,來具體說明一下。為了更清楚的說明這個問題,在選用資料庫方面採用了二種當前比較典型的資料庫,其一是本機資料庫--Access 2000,另外一個是遠端資料庫--SQL SERVER 7.0。首先介紹如何用Visual C#來添加Access 2000資料庫的記錄。

一.用Visual C#來添加Access 2000資料庫的記錄
(一).程式設計和啟動並執行環境設定:
(1)視窗2000伺服器版
(2)Microsoft Data Acess Component 2.6 以上版本 ( MDAC 2.6 )
(3)本文程式使用的資料庫的介紹:

程式中使用的資料庫名稱為sample.mdb,在此資料庫中有一張資料表books。此資料表的結構如下:
欄位名稱欄位類型代表意思Bookid數字序號booktitle文本書籍名稱bookauthor文本書籍作者bookprice數字價格bookstock數字書架號
(二).程式設計痛點和應該注意的問題:
如何正確的往資料庫中添加記錄是本文要討論的一個重點和痛點,下面就是解決這一問題的具體思路:
(1)建立並開啟一個 OleDbConnection對象。
(2)建立一個插入一條記錄的SQL語句。
(3)建立一個OleDbCommand對象。
(4)通過此OleDbCommand對象完成對插入一條記錄到資料庫的操作。
以下是在程式中實現的具體語句:
string strConn = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = sample.mdb " ;
OleDbConnection myConn = new OleDbConnection ( strConn ) ;
myConn.Open ( ) ;
string strInsert = " INSERT INTO books ( bookid , booktitle , bookauthor , bookprice , bookstock ) VALUES ( " ;
strInsert += t_bookid.Text + ", '" ;
strInsert += t_booktitle.Text + "', '" ;
strInsert += t_bookauthor.Text + "', " ;
strInsert += t_bookprice.Text + ", " ;
strInsert += t_bookstock.Text + ")" ;
OleDbCommand inst = new OleDbCommand ( strInsert , myConn ) ;
inst.ExecuteNonQuery ( ) ;
myConn.Close ( ) ;

(三).用Visual C#來插入記錄的程式原始碼( add.cs )和執行後的介面:
下圖是add.cs編譯後的執行介面:

add.cs來源程式代碼:
using System ;
using System.Drawing ;
using System.ComponentModel ;
using System.Windows.Forms ;
using System.Data.OleDb ;
using System.Data ;
//匯入程式中使用到的名稱空間
public class DataAdd : Form {
private Button lastrec ;
private Button nextrec ;
private Button previousrec ;
private Button firstrec ;
private Container components ;
private Label title ;
private Button t_new ;
private Button save ;
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 DataSet myDataSet ;
private BindingManagerBase myBind ;
//定義在程式中要使用的組件
public DataAdd ( ) {
//串連到一個資料庫
GetConnected ( ) ;
// 對表單中所需要的內容進行初始化
InitializeComponent ( );
}
//釋放程式使用過的所以資源
public override void Dispose ( ) {
base.Dispose ( ) ;
components.Dispose ( ) ;
}
public static void Main ( ) {
Application.Run ( new DataAdd ( ) ) ;
}
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 得到一個資料集
OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom , myConn ) ;
//把Dataset綁定books資料表
myCommand.Fill ( myDataSet , "books" ) ;
//關閉此OleDbConnection
myConn.Close ( ) ;
}
catch ( Exception e )
{
MessageBox.Show ( "串連錯誤! " + e.ToString ( ) , "錯誤" ) ;
}
}
private void InitializeComponent ( )
{
components = new System.ComponentModel.Container ( ) ;
nextrec = new Button ( ) ;
lastrec = new Button ( ) ;
previousrec = new Button ( ) ;
firstrec = new Button ( ) ;
t_bookprice = new TextBox ( ) ;
l_booktitle = new Label ( ) ;
l_bookprice = new Label ( ) ;
l_bookauthor = new Label ( ) ;
t_bookid = new TextBox ( ) ;
save = new Button ( ) ;
title = new Label ( ) ;
t_bookauthor = new TextBox ( ) ;
t_booktitle = new TextBox ( ) ;
t_new = new Button ( ) ;
l_bookstock = new Label ( ) ;
t_bookstock = new TextBox ( ) ;
l_bookid = new Label ( ) ;
//以下是對資料瀏覽的四個按鈕進行初始化
firstrec.Location = new System.Drawing.Point ( 65 , 312 ) ;
firstrec.ForeColor = System.Drawing.Color.Black ;
firstrec.Size = new System.Drawing.Size ( 40 , 24 ) ;
firstrec.Font = new System.Drawing.Font("仿宋", 8f );
firstrec.Text = "首記錄";
firstrec.Click += new System.EventHandler(GoFirst);
previousrec.Location = new System.Drawing.Point ( 135 , 312 ) ;
previousrec.ForeColor = System.Drawing.Color.Black ;
previousrec.Size = new System.Drawing.Size(40, 24) ;
previousrec.Font = new System.Drawing.Font ( "仿宋" , 8f ) ;
previousrec.Text = "上一條" ;
previousrec.Click += new System.EventHandler ( GoPrevious ) ;
nextrec.Location = new System.Drawing.Point ( 205 , 312 );
nextrec.ForeColor = System.Drawing.Color.Black ;
nextrec.Size = new System.Drawing.Size ( 40 , 24 ) ;
nextrec.Font = new System.Drawing.Font ( "仿宋" , 8f ) ;
nextrec.Text = "下一條" ;
nextrec.Click += new System.EventHandler ( GoNext );
lastrec.Location = new System.Drawing.Point ( 275 , 312 ) ;
lastrec.ForeColor = System.Drawing.Color.Black ;
lastrec.Size = new System.Drawing.Size ( 40 , 24 ) ;
lastrec.Font = new System.Drawing.Font ( "仿宋" , 8f ) ;
lastrec.Text = "尾記錄" ;
lastrec.Click += new System.EventHandler ( GoLast ) ;
//以下是對顯示標籤進行初始化
l_bookid.Location = new System.Drawing.Point ( 24 , 56 ) ;
l_bookid.Text = "書本序號:" ;
l_bookid.Size = new System.Drawing.Size ( 112, 20 ) ;
l_bookid.Font = new System.Drawing.Font ( "仿宋" , 10f ) ;
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 ( 112 , 20 ) ;
l_booktitle.Font = new System.Drawing.Font ( "仿宋" , 10f ) ;
l_booktitle.TextAlign = System.Drawing.ContentAlignment.MiddleCenter ;
l_bookprice.Location = new System.Drawing.Point ( 24 , 212 ) ;
l_bookprice.Text = "價 格:" ;
l_bookprice.Size = new System.Drawing.Size ( 112 , 20 ) ;
l_bookprice.Font = new System.Drawing.Font ( "仿宋" , 10f ) ;
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 ( 112 , 20 ) ;
l_bookstock.Font = new System.Drawing.Font ( "仿宋" , 10f ) ;
l_bookstock.TabIndex = 16 ;
l_bookstock.TextAlign = System.Drawing.ContentAlignment.MiddleCenter ;
l_bookauthor.Location = new System.Drawing.Point ( 24 , 160 ) ;
l_bookauthor.Text = "作 者:" ;
l_bookauthor.Size = new System.Drawing.Size ( 112 , 20 ) ;
l_bookauthor.Font = new System.Drawing.Font ( "仿宋" , 10f ) ;
l_bookauthor.TextAlign = System.Drawing.ContentAlignment.MiddleCenter ;
title.Location = new System.Drawing.Point ( 32 , 16 ) ;
title.Text = "利用Vsiual C#來增加資料記錄!" ;
title.Size = new System.Drawing.Size ( 336 , 24 ) ;
title.ForeColor = System.Drawing.Color.Green ;
title.Font = new System.Drawing.Font ( "仿宋" , 14f , System.Drawing.FontStyle.Bold ) ;
//以下是對為顯示資料記錄而設定的標籤和文字框進行初始化,並把記錄綁定在不同的綁定到文字框"Text"屬性上
t_bookid.Location = new System.Drawing.Point ( 184 , 56 ) ;
t_bookid.Size = new System.Drawing.Size ( 80 , 20 ) ;
t_bookid.DataBindings.Add ( "Text" , myDataSet , "books.bookid" ) ;
t_bookstock.Location = new System.Drawing.Point ( 184 , 264 ) ;
t_bookstock.Size = new System.Drawing.Size ( 80 , 20 ) ;
t_bookstock.DataBindings.Add ( "Text" , myDataSet , "books.bookstock" ) ;
t_booktitle.Location = new System.Drawing.Point ( 184 , 108 ) ;
t_booktitle.Size = new System.Drawing.Size ( 176 , 20 ) ;
t_booktitle.DataBindings.Add( "Text" , myDataSet , "books.booktitle" ) ;
t_bookprice.Location = new System.Drawing.Point ( 184 , 212 ) ;
t_bookprice.Size = new System.Drawing.Size ( 80 , 20 ) ;
t_bookprice.DataBindings.Add ( "Text" , myDataSet , "books.bookprice" ) ;
t_bookauthor.Location = new System.Drawing.Point ( 184 , 160 ) ;
t_bookauthor.Size = new System.Drawing.Size ( 128 , 20 ) ;
t_bookauthor.DataBindings.Add ( "Text" , myDataSet , "books.bookauthor" ) ;
t_new.Location = new System.Drawing.Point ( 62 , 354 ) ;
t_new.Size = new System.Drawing.Size ( 96 , 32 ) ;
t_new.Text = "建立記錄" ;
t_new.Click += new System.EventHandler ( t_newClick ) ;
save.Location = new System.Drawing.Point ( 222 , 354 ) ;
save.Size = new System.Drawing.Size ( 96 , 32 ) ;
save.TabIndex = 4 ;
save.Text = "儲存記錄" ;
save.Click += new System.EventHandler ( saveClick ) ;
this.Text = "利用Vsiual C#來增加資料記錄的程式視窗!" ;
this.AutoScaleBaseSize = new System.Drawing.Size ( 5 , 13 ) ;
this.FormBorderStyle = FormBorderStyle.Fixed3D ;
this.ClientSize = new System.Drawing.Size ( 390 , 400 ) ;
//在表單中加入下列組件
this.Controls.Add ( lastrec ) ;
this.Controls.Add ( nextrec ) ;
this.Controls.Add ( previousrec ) ;
this.Controls.Add ( firstrec ) ;
this.Controls.Add ( title ) ;
this.Controls.Add ( t_new ) ;
this.Controls.Add ( save ) ;
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 ) ;
//把對象DataSet和"books"資料表綁定到此myBind對象
myBind= this.BindingContext [ myDataSet , "books" ] ;
}
protected void saveClick ( object sender , System.EventArgs e )
{
try
{
//判斷所有欄位是否添完,添完則執行,反之彈出提示
if ( t_bookid.Text != "" && t_booktitle.Text != "" && t_bookauthor.Text != "" && t_bookprice.Text != "" && t_bookstock.Text != "" )
{
string strConn = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = sample.mdb " ;
OleDbConnection myConn = new OleDbConnection ( strConn ) ;
myConn.Open ( ) ;
string strInsert = " INSERT INTO books ( bookid , booktitle , bookauthor , bookprice , bookstock ) VALUES ( " ;
strInsert += t_bookid.Text + ", '" ;
strInsert += t_booktitle.Text + "', '" ;
strInsert += t_bookauthor.Text + "', " ;
strInsert += t_bookprice.Text + ", " ;
strInsert += t_bookstock.Text + ")" ;
OleDbCommand inst = new OleDbCommand ( strInsert , myConn ) ;
inst.ExecuteNonQuery ( ) ;
myConn.Close ( ) ;
}
else
{
MessageBox.Show ( "必須填滿所有欄位值!" , "錯誤!" ) ;
}
}
catch ( Exception ed )
{
MessageBox.Show ( "儲存資料記錄發生 " + ed.ToString ( ) , "錯誤!" ) ;
}
}
protected void t_newClick ( object sender , System.EventArgs e )
{
t_bookid.Text = "" ;
t_booktitle.Text = "" ;
t_bookauthor.Text = "" ;
t_bookprice.Text = "" ;
t_bookstock.Text = "" ;
}
//按鈕"尾記錄"對象事件程式
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 ;
}
}

成功編譯上面代碼,就可以往Access 2000資料庫裡面插入記錄了。



相關文章

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 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。