用delphi或者vb編程,在對(duì)數(shù)據(jù)庫(kù)中的記錄進(jìn)行操作的時(shí)候,經(jīng)常用到一個(gè)名稱為數(shù)據(jù)導(dǎo)航器的組件,通過(guò)這個(gè)組件,可以非常方便的實(shí)現(xiàn)對(duì)已經(jīng)綁定到此組件的數(shù)據(jù)表中的記錄進(jìn)行瀏覽。就是所謂的上一條記錄、下一條記錄、首記錄、尾記錄等。那么在visual c#是否也存在這樣的組件呢?答案是否定的。但由于visual c#有著強(qiáng)大的數(shù)據(jù)庫(kù)處理能力,所以可以比較方便的做一個(gè)類似于此組件的程序。本文就是來(lái)介紹此程序的具體制作過(guò)程。
一、 程序的主要功能介紹:
程序打開本地acess數(shù)據(jù)庫(kù)(sample.mdb)中的book數(shù)據(jù)表,然后把book數(shù)據(jù)表中的
字段綁定到程序提供的文本框中,顯示出來(lái)。通過(guò)程序中的四個(gè)按鈕"首記錄"、"尾記錄"、"上一條"、"下一條",實(shí)現(xiàn)對(duì)book數(shù)據(jù)表中的記錄瀏覽。程序的運(yùn)行界面如下:
圖01:對(duì)數(shù)據(jù)表中記錄瀏覽程序的運(yùn)行界面
二、程序設(shè)計(jì)和運(yùn)行的環(huán)境設(shè)置:
(1)視窗2000服務(wù)器版
(2)microsoft acess data component 2.6 ( madc 2.6 )
三、程序設(shè)計(jì)難點(diǎn)和應(yīng)該注意的問(wèn)題:
(1)如何實(shí)現(xiàn)把數(shù)據(jù)表中的字段用文本框來(lái)顯示:
如果直接把字段的值賦值給文本框,這時(shí)如果用"下一條"等按鈕來(lái)瀏覽數(shù)據(jù)記錄的時(shí)候,文本框的值是不會(huì)變化的。如何讓文本框根據(jù)數(shù)據(jù)表中的記錄指針來(lái)動(dòng)態(tài)的顯示要字段值,這是本文的一個(gè)重點(diǎn),也是一個(gè)難點(diǎn)。
本文是通過(guò)把數(shù)據(jù)表中的字段值綁定到文本框的"text"屬性上,來(lái)實(shí)現(xiàn)動(dòng)態(tài)顯示字段數(shù)值的。實(shí)現(xiàn)這種處理要用到文本框的databindings屬性和其中的add方法。具體語(yǔ)法如下:
文本組件名稱.databindings.add ( "text" , dataset對(duì)象 , 數(shù)據(jù)表和字段名稱 ) ;
在程序具體如下:
t_bookid.databindings.add ( "text" , mydataset , "books.bookid" ) ;
這樣就可以根據(jù)記錄指針來(lái)實(shí)現(xiàn)要顯示的字段值了。
(2)如何改變記錄指針:
只有掌握如何改變記錄指針,才可以隨心所欲的瀏覽記錄。visual c#改變記錄指針是通過(guò)一個(gè)命叫bindingmanagerbase對(duì)象來(lái)實(shí)現(xiàn)的。此對(duì)象封裝在名稱空間system.windows.froms中。bindingmanagerbase對(duì)象是一個(gè)抽象的對(duì)象,管理所有綁定的同類的數(shù)據(jù)源和數(shù)據(jù)成員。在程序設(shè)計(jì)中主要用到bindingmanagerbase對(duì)象中的二個(gè)屬性,即:position屬性和count屬性。第一個(gè)屬性是記錄了數(shù)據(jù)集的當(dāng)前指針,后一個(gè)屬性是當(dāng)前數(shù)據(jù)集中的記錄總數(shù)。由此可以得到改變記錄指針的四個(gè)按鈕對(duì)應(yīng)的程序代碼:
i>.首記錄:
mybind.position = 0 ;
ii>.尾記錄:
mybind.position = mybind.count - 1 ;
iii>.下一條記錄和操作后運(yùn)行界面:
if ( mybind.position == mybind.count -1 )
messagebox.show ( "已經(jīng)到了最后一條記錄!" ) ;
else
mybind.position += 1 ;
iv>.上一條記錄和操作后運(yùn)行界面:
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 ( )
{
//連接到一個(gè)數(shù)據(jù)庫(kù)
getconnected ( ) ;
// 對(duì)窗體中所需要的內(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)建一個(gè) 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)建一個(gè) dataset
mydataset = new dataset ( ) ;
myconn.open ( ) ;
//用 oledbdataadapter 得到一個(gè)數(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 ( "連接錯(cuò)誤! " + e.tostring ( ) , "錯(cuò)誤" ) ;
}
}
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 ( ) ;
//以下是對(duì)數(shù)據(jù)瀏覽的四個(gè)按鈕進(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 ) ;
//以下是對(duì)為顯示數(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 = "書本序號(hào):" ;
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 = "價(jià) 格:" ;
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 = "書 架 號(hào):" ;
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 ;
//對(duì)窗體進(jìn)行設(shè)定
this.text = "用c#做瀏覽數(shù)據(jù)庫(kù)中記錄的程序!";
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 ) ;
//把對(duì)象dataset和"books"數(shù)據(jù)表綁定到此mybind對(duì)象
mybind= this.bindingcontext [ mydataset , "books" ] ;
}
//按鈕"尾記錄"對(duì)象事件程序
protected void golast ( object sender , system.eventargs e )
{
mybind.position = mybind.count - 1 ;
}
//按鈕"下一條"對(duì)象事件程序
protected void gonext ( object sender , system.eventargs e )
{
if ( mybind.position == mybind.count -1 )
messagebox.show ( "已經(jīng)到了最后一條記錄!" ) ;
else
mybind.position += 1 ;
}
//按鈕"上一條"對(duì)象事件程序
protected void goprevious ( object sender , system.eventargs e )
{
if ( mybind.position == 0 )
messagebox.show ( "已經(jīng)到了第一條記錄!" ) ;
else
mybind.position -= 1 ;
}
//按鈕"首記錄"對(duì)象事件程序
protected void gofirst ( object sender , system.eventargs e )
{
mybind.position = 0 ;
}
}
五.總結(jié):
本文的重點(diǎn)就在于如何用visual c#改變數(shù)據(jù)集的記錄指針和如何讓文本框根據(jù)記錄指針的變化而改變顯示內(nèi)容。雖然此類處理在visual c#比起用其他語(yǔ)言要顯得麻煩些。但對(duì)于程序設(shè)計(jì)人員卻更靈活了,使得程序設(shè)計(jì)人員有了更大的發(fā)展空間。
本文來(lái)源于網(wǎng)頁(yè)設(shè)計(jì)愛好者web開發(fā)社區(qū)http://www.html.org.cn收集整理,歡迎訪問(wèn)。