直播中
一、 程序的主要功能介紹:
程序打開本地Acess數(shù)據(jù)庫(sample.mdb)中的book數(shù)據(jù)表,然后把book數(shù)據(jù)表中的
字段綁定到程序提供的文本框中,顯示出來。通過程序中的四個按鈕"首記錄"、"尾記錄"、"上一條"、"下一條",實現(xiàn)對book數(shù)據(jù)表中的記錄瀏覽。程序的運行界面如下:
圖01:對數(shù)據(jù)表中記錄瀏覽程序的運行界面
二、程序設(shè)計和運行的環(huán)境設(shè)置:
(1)視窗2000服務(wù)器版
(2)Microsoft Acess Data Component 2.6 ( MADC 2.6 )
三、程序設(shè)計難點和應(yīng)該注意的問題:
(1)如何實現(xiàn)把數(shù)據(jù)表中的字段用文本框來顯示:
如果直接把字段的值賦值給文本框,這時如果用"下一條"等按鈕來瀏覽數(shù)據(jù)記錄的時候,文本框的值是不會變化的。如何讓文本框根據(jù)數(shù)據(jù)表中的記錄指針來動態(tài)的顯示要字段值,這是本文的一個重點,也是一個難點。
本文是通過把數(shù)據(jù)表中的字段值綁定到文本框的"Text"屬性上,來實現(xiàn)動態(tài)顯示字段數(shù)值的。實現(xiàn)這種處理要用到文本框的DataBindings屬性和其中的Add方法。具體語法如下:
文本組件名稱.DataBindings.Add ( "Text" , DataSet對象 , 數(shù)據(jù)表和字段名稱 ) ;
在程序具體如下:
t_bookid.DataBindings.Add ( "Text" , myDataSet , "books.bookid" ) ;
這樣就可以根據(jù)記錄指針來實現(xiàn)要顯示的字段值了。
(2)如何改變記錄指針:
只有掌握如何改變記錄指針,才可以隨心所欲的瀏覽記錄。Visual C#改變記錄指針是通過一個命叫BindingManagerBase對象來實現(xiàn)的。此對象封裝在名稱空間System.Windows.Froms中。BindingManagerBase對象是一個抽象的對象,管理所有綁定的同類的數(shù)據(jù)源和數(shù)據(jù)成員。在程序設(shè)計中主要用到BindingManagerBase對象中的二個屬性,即:Position屬性和Count屬性。第一個屬性是記錄了數(shù)據(jù)集的當(dāng)前指針,后一個屬性是當(dāng)前數(shù)據(jù)集中的記錄總數(shù)。由此可以得到改變記錄指針的四個按鈕對應(yīng)的程序代碼:
i>.首記錄:
myBind.Position = 0 ;
ii>.尾記錄:
myBind.Position = myBind.Count - 1 ;
iii>.下一條記錄和操作后運行界面:
if ( myBind.Position == myBind.Count -1 )
MessageBox.Show ( "已經(jīng)到了最后一條記錄!" ) ;
else
myBind.Position += 1 ;
iV>.上一條記錄和操作后運行界面:
if ( myBind.Position == 0 )
MessageBox.Show ( "已經(jīng)到了第一條記錄!" ) ;
else
myBind.Position -= 1 ;
四.程序源代碼:
using System ;
using System.Drawing ;
using System.ComponentModel ;
using System.Windows.Forms ;
using System.Data.OleDb ;
using System.Data ;
public class DataView : Form {
private System.ComponentModel.Container components ;
private Button lastrec ;
private Button nextrec ;
private Button previousrec ;
private Button firstrec ;
private TextBox t_books ;
private TextBox t_bookprice ;
private TextBox t_bookauthor ;
private TextBox t_booktitle ;
private TextBox t_bookid ;
private Label l_books ;
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 ;
public DataView ( )
{
//連接到一個數(shù)據(jù)庫
GetConnected ( ) ;
// 對窗體中所需要的內(nèi)容進(jìn)行初始化
InitializeComponent ( );
}
public override void Dispose ( ) {
base.Dispose ( ) ;
components.Dispose ( ) ;
}
public static void Main ( ) {
Application.Run ( new DataView ( ) ) ;
}
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 得到一個數(shù)據(jù)集
OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom , myConn ) ;
//把Dataset綁定books數(shù)據(jù)表
myCommand.Fill ( myDataSet , "books" ) ;
//關(guān)閉此OleDbConnection
myConn.Close ( ) ;
}
catch ( Exception e )
{
MessageBox.Show ( "連接錯誤! " + e.ToString ( ) , "錯誤" ) ;
}
}
private void InitializeComponent ( )
{
this.components = new System.ComponentModel.Container ( ) ;
this.t_bookid = new TextBox ( ) ;
this.nextrec = new Button ( ) ;
this.lastrec = new Button ( ) ;
this.l_bookid = new Label ( ) ;
this.t_books = new TextBox ( ) ;
this.t_booktitle = new TextBox ( ) ;
this.t_bookprice = new TextBox ( ) ;
this.firstrec = new Button ( ) ;
this.l_booktitle = new Label ( ) ;
this.l_bookprice = new Label ( ) ;
this.l_books = new Label ( ) ;
this.previousrec = new Button ( ) ;
this.l_bookauthor = new Label ( ) ;
this.t_bookauthor = new TextBox ( ) ;
this.label1 = new Label ( ) ;
//以下是對數(shù)據(jù)瀏覽的四個按鈕進(jìn)行初始化
firstrec.Location = new System.Drawing.Point ( 55 , 312 ) ;
firstrec.ForeColor = System.Drawing.Color.Black ;
firstrec.Size = new System.Drawing.Size ( 40 , 24 ) ;
firstrec.TabIndex = 5 ;
firstrec.Font = new System.Drawing.Font("仿宋", 8f );
firstrec.Text = "首記錄";
firstrec.Click += new System.EventHandler(GoFirst);
previousrec.Location = new System.Drawing.Point ( 125 , 312 ) ;
previousrec.ForeColor = System.Drawing.Color.Black ;
previousrec.Size = new System.Drawing.Size(40, 24) ;
previousrec.TabIndex = 6 ;
previousrec.Font = new System.Drawing.Font ( "仿宋" , 8f ) ;
previousrec.Text = "上一條" ;
previousrec.Click += new System.EventHandler ( GoPrevious ) ;
nextrec.Location = new System.Drawing.Point ( 195 , 312 );
nextrec.ForeColor = System.Drawing.Color.Black ;
nextrec.Size = new System.Drawing.Size ( 40 , 24 ) ;
nextrec.TabIndex = 7 ;
nextrec.Font = new System.Drawing.Font ( "仿宋" , 8f ) ;
nextrec.Text = "下一條" ;
nextrec.Click += new System.EventHandler ( GoNext );
lastrec.Location = new System.Drawing.Point ( 265 , 312 ) ;
lastrec.ForeColor = System.Drawing.Color.Black ;
lastrec.Size = new System.Drawing.Size ( 40 , 24 ) ;
lastrec.TabIndex = 8 ;
lastrec.Font = new System.Drawing.Font ( "仿宋" , 8f ) ;
lastrec.Text = "尾記錄" ;
lastrec.Click += new System.EventHandler ( GoLast ) ;
//以下是對為顯示數(shù)據(jù)記錄而設(shè)定的標(biāo)簽和文本框進(jìn)行初始化,并把記錄綁定在不同的綁定到文本框"Text"屬性上
t_bookid.Location = new System.Drawing.Point ( 184 , 56 ) ;
t_bookid.TabIndex = 9 ;
t_bookid.Size = new System.Drawing.Size ( 80 , 20 ) ;
t_bookid.DataBindings.Add ( "Text" , myDataSet , "books.bookid" ) ;
t_books.Location = new System.Drawing.Point ( 184 , 264 ) ;
t_books.TabIndex = 10 ;
t_books.Size = new System.Drawing.Size ( 80 , 20 ) ;
t_books.DataBindings.Add ( "Text" , myDataSet , "books.bookstock" ) ;
t_booktitle.Location = new System.Drawing.Point ( 184 , 108 ) ;
t_booktitle.TabIndex = 11 ;
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.TabIndex = 12 ;
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.TabIndex = 18 ;
t_bookauthor.Size = new System.Drawing.Size ( 128 , 20 ) ;
t_bookauthor.DataBindings.Add ( "Text" , myDataSet , "books.bookauthor" ) ;
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.TabIndex = 13 ;
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.TabIndex = 14 ;
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.TabIndex = 15 ;
l_bookprice.TextAlign = System.Drawing.ContentAlignment.MiddleCenter ;
l_books.Location = new System.Drawing.Point ( 24 , 264 ) ;
l_books.Text = "書 架 號:" ;
l_books.Size = new System.Drawing.Size ( 112 , 20 ) ;
l_books.Font = new System.Drawing.Font ( "仿宋" , 10f ) ;
l_books.TabIndex = 16 ;
l_books.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.TabIndex = 17 ;
l_bookauthor.TextAlign = System.Drawing.ContentAlignment.MiddleCenter ;
label1.Location = new System.Drawing.Point ( 49 , 8 ) ;
label1.Text = "瀏覽書籍信息" ;
label1.Size = new System.Drawing.Size ( 296 , 24 ) ;
label1.ForeColor = System.Drawing.Color.Green ;
label1.Font = new System.Drawing.Font ( "仿宋" , 15f ) ;
label1.TabIndex = 19 ;
//對窗體進(jìn)行設(shè)定
this.Text = "用C#做瀏覽數(shù)據(jù)庫中記錄的程序!";
this.AutoScaleBaseSize = new System.Drawing.Size ( 5 , 13 ) ;
this.FormBorderStyle = FormBorderStyle.FixedSingle ;
this.ClientSize = new System.Drawing.Size ( 394 , 375 ) ;
//在窗體中加入組件
this.Controls.Add ( lastrec ) ;
this.Controls.Add ( nextrec ) ;
this.Controls.Add ( previousrec ) ;
this.Controls.Add ( firstrec ) ;
this.Controls.Add ( t_books ) ;
this.Controls.Add ( t_bookprice ) ;
this.Controls.Add ( t_bookauthor ) ;
this.Controls.Add ( t_booktitle ) ;
this.Controls.Add ( t_bookid ) ;
this.Controls.Add ( l_books ) ;
this.Controls.Add ( l_bookprice ) ;
this.Controls.Add ( l_bookauthor ) ;
this.Controls.Add ( l_booktitle ) ;
this.Controls.Add ( l_bookid ) ;
this.Controls.Add ( label1 ) ;
//把對象DataSet和"books"數(shù)據(jù)表綁定到此myBind對象
myBind= this.BindingContext [ myDataSet , "books" ] ;
}
//按鈕"尾記錄"對象事件程序
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 ( "已經(jīng)到了最后一條記錄!" ) ;
else
myBind.Position += 1 ;
}
//按鈕"上一條"對象事件程序
protected void GoPrevious ( object sender , System.EventArgs e )
{
if ( myBind.Position == 0 )
MessageBox.Show ( "已經(jīng)到了第一條記錄!" ) ;
else
myBind.Position -= 1 ;
}
//按鈕"首記錄"對象事件程序
protected void GoFirst ( object sender , System.EventArgs e )
{
myBind.Position = 0 ;
}
}
五.總結(jié):
本文的重點就在于如何用Visual C#改變數(shù)據(jù)集的記錄指針和如何讓文本框根據(jù)記錄指針的變化而改變顯示內(nèi)容。雖然此類處理在Visual C#比起用其他語言要顯得麻煩些。但對于程序設(shè)計人員卻更靈活了,使得程序設(shè)計人員有了更大的發(fā)展空間。