国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

Visual C#中輕松瀏覽數據庫記錄

2024-07-21 02:25:06
字體:
來源:轉載
供稿:網友

最大的網站源碼資源下載站,

用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#比起用其他語言要顯得麻煩些。但對于程序設計人員卻更靈活了,使得程序設計人員有了更大的發展空間。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 肥西县| 彰化市| 池州市| 延长县| 沂水县| 新泰市| 瑞安市| 荃湾区| 瑞金市| 九龙县| 桂东县| 涪陵区| 清流县| 周宁县| 丰都县| 台中县| 金坛市| 郓城县| 邻水| 霍山县| 招远市| 年辖:市辖区| 大兴区| 都江堰市| 同心县| 鄯善县| 绍兴市| 杨浦区| 永嘉县| 建宁县| 略阳县| 青阳县| 鹰潭市| 靖江市| 祁连县| 汕头市| 黔西县| 育儿| 邳州市| 洛川县| 巴中市|