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 ;
}
}
|