用Visual C#中輕鬆瀏覽資料庫記錄(轉)

來源:互聯網
上載者:User
visual|資料|資料庫 用Delphi或者VB編程,在對資料庫中的記錄進行操作的時候,經常用到一個名稱為資料導航器的組件,通過這個組件,可以非常方便的實現對已經綁定到此組件的資料表中的記錄進行瀏覽。就是所謂的上一條記錄、下一條記錄、首記錄、尾記錄等。那麼在Visual C#是否也存在這樣的組件呢?答案是否定的。但由於Visual C#有著強大的資料庫處理能力,所以可以比較方便的做一個類似於此組件的程式。本文就是來介紹此程式的具體製作過程。

一、 程式的主要功能介紹:
程式開啟本地Acess資料庫(sample.mdb)中的book資料表,然後把book資料表中的
欄位綁定到程式提供的文字框中,顯示出來。通過程式中的四個按鈕"首記錄"、"尾記錄"、"上一條"、"下一條",實現對book資料表中的記錄瀏覽。程式的運行介面如下:

圖01:對資料表中記錄瀏覽程式的運行介面

二、程式設計和啟動並執行環境設定:
(1)視窗2000伺服器版
(2)Microsoft Acess Data Component 2.6 ( MADC 2.6 )

三、程式設計痛點和應該注意的問題:
(1)如何?把資料表中的欄位用文字框來顯示:
如果直接把欄位的值賦值給文字框,這時如果用"下一條"等按鈕來瀏覽資料記錄的時候,文字框的值是不會變化的。如何讓文字框根據資料表中的記錄指標來動態顯示要欄位值,這是本文的一個重點,也是一個痛點。
本文是通過把資料表中的欄位值綁定到文字框的"Text"屬性上,來實現動態顯示欄位數值的。實現這種處理要用到文字框的DataBindings屬性和其中的Add方法。具體文法如下:
文本組件名稱.DataBindings.Add ( "Text" , DataSet對象 , 資料表和欄位名稱 ) ;
在程式具體如下:
t_bookid.DataBindings.Add ( "Text" , myDataSet , "books.bookid" ) ;

這樣就可以根據記錄指標來實現要顯示的欄位值了。
(2)如何改變記錄指標:
只有掌握如何改變記錄指標,才可以隨心所欲的瀏覽記錄。Visual C#改變記錄指標是通過一個命叫BindingManagerBase對象來實現的。此對象封裝在名稱空間System.Windows.Froms中。BindingManagerBase對象是一個抽象的對象,管理所有綁定的同類的資料來源和資料成員。在程式設計中主要用到BindingManagerBase對象中的二個屬性,即:Position屬性和Count屬性。第一個屬性是記錄了資料集的當前指標,後一個屬性是當前資料集中的記錄總數。由此可以得到改變記錄指標的四個按鈕對應的程式碼:
i>.首記錄:
myBind.Position = 0 ;
ii>.尾記錄:
myBind.Position = myBind.Count - 1 ;
iii>.下一條記錄和操作後運行介面:
if ( myBind.Position == myBind.Count -1 )
MessageBox.Show ( "已經到了最後一條記錄!" ) ;
else
myBind.Position += 1 ;

iV>.上一條記錄和操作後運行介面:
if ( myBind.Position == 0 )
MessageBox.Show ( "已經到了第一條記錄!" ) ;
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 ( )
{
//串連到一個資料庫
GetConnected ( ) ;
// 對表單中所需要的內容進行初始化
InitializeComponent ( );
}
public override void Dispose ( ) {
base.Dispose ( ) ;
components.Dispose ( ) ;
}
public static void Main ( ) {
Application.Run ( new DataView ( ) ) ;
}
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 ( )
{
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 ( ) ;

//以下是對資料瀏覽的四個按鈕進行初始化
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 ) ;
//以下是對為顯示資料記錄而設定的標籤和文字框進行初始化,並把記錄綁定在不同的綁定到文字框"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 ;
//對表單進行設定
this.Text = "用C#做瀏覽資料庫中記錄的程式!";
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"資料表綁定到此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 ( "已經到了最後一條記錄!" ) ;
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 ;
}
}


五.總結:
本文的重點就在於如何用Visual C#改變資料集的記錄指標和如何讓文字框根據記錄指標的變化而改變顯示內容。雖然此類處理在Visual C#比起用其他語言要顯得麻煩些。但對於程式設計人員卻更靈活了,使得程式設計人員有了更大的發展空間。


相關文章

聯繫我們

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

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

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.