首页 > 开发 > 综合 > 正文

用Visual C#中轻松浏览数据库记录

2020-02-03 13:45:46
字体:
来源:转载
供稿:网友

用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#比起用其他语言要显得麻烦些。但对于程序设计人员却更灵活了,使得程序设计人员有了更大的发展空间。



发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表