using system ;
using system.drawing ;
using system.componentmodel ;
using system.windows.forms ;
using system.data.oledb ;
using system.data ;
public class dataedit : form { private system.componentmodel.container components ;
private button delete ;
private button update ;
private button lastrec ;
private button nextrec ;
private button previousrec ;
private button firstrec ;
private textbox t_bookstock ;
private textbox t_bookprice ;
private textbox t_bookauthor ;
private textbox t_booktitle ;
private textbox t_bookid ;
private label l_bookstock ;
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 ;
private bool isbound = false ;
//定义此变量,是判断组件是否已经绑定数据表中的字段
public dataedit ( ) {
// 对窗体中所需要的内容进行初始化
initializecomponent ( ) ;
//连接到一个数据库
getconnected ( ) ;
}
//清除程序中用到的所有资源
public override void dispose ( ) {
base.dispose ( ) ;
components.dispose ( ) ;
}
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 mycommand = new oledbdataadapter ( strcom , myconn ) ;
mycommand.fill ( mydataset , "books" ) ;
myconn.close ( ) ;
//判断数据字段是否绑定到 textboxes
if ( !isbound )
{
//以下是为显示数据记录而把数据表的某个字段绑定在不同的绑定到文本框"text"属性上
t_bookid.databindings.add ( "text" , mydataset , "books.bookid" ) ;
t_booktitle.databindings.add ( "text" , mydataset , "books.booktitle" ) ;
t_bookauthor.databindings.add ( "text" , mydataset , "books.bookauthor" ) ;
t_bookprice.databindings.add ( "text" , mydataset , "books.bookprice" ) ;
t_bookstock.databindings.add ( "text" , mydataset , "books.bookstock" ) ;
//设定 bindingmanagerbase
//把对象dataset和"books"数据表绑定到此mybind对象
mybind = this.bindingcontext [ mydataset , "books" ] ;
isbound = true ;
}
}
catch ( exception e )
{
messagebox.show ( "连接数据库发生错误为:" + e.tostring ( ) , "错误!" ) ;
}
}
public static void main ( ) {
application.run ( new dataedit ( ) ) ;
}
private void initializecomponent ( )
{
this.components = new system.componentmodel.container ( ) ;
this.t_bookid = new textbox ( ) ;
this.previousrec = new button ( ) ;
this.l_bookauthor = new label ( ) ;
this.delete = new button ( ) ;
this.t_booktitle = new textbox ( ) ;
this.t_bookauthor = new textbox ( ) ;
this.t_bookprice = new textbox ( ) ;
this.l_bookprice = new label ( ) ;
this.t_bookstock = new textbox ( ) ;
this.l_bookstock = new label ( ) ;
this.l_booktitle = new label ( ) ;
this.update = new button ( ) ;
this.nextrec = new button ( ) ;
this.lastrec = new button ( ) ;
this.firstrec = new button ( ) ;
this.label1 = new label ( ) ;
this.l_bookid = new label ( ) ;
t_bookid.location = new system.drawing.point ( 184 , 56 ) ;
t_bookid.size = new system.drawing.size ( 80 , 20 ) ;
t_booktitle.location = new system.drawing.point ( 184 , 108 ) ;
t_booktitle.size = new system.drawing.size ( 176 , 20 ) ;
t_bookauthor.location = new system.drawing.point ( 184 , 160 ) ;
t_bookauthor.size = new system.drawing.size ( 128 , 20 ) ;
t_bookprice.location = new system.drawing.point ( 184 , 212 ) ;
t_bookprice.size = new system.drawing.size ( 80 , 20 ) ;
t_bookstock.location = new system.drawing.point ( 184 , 264 ) ;
t_bookstock.size = new system.drawing.size ( 80 , 20 ) ;
//以下是设定在程序中使用到的label属性
l_bookid.location = new system.drawing.point ( 24 , 56 ) ;
l_bookid.text = "序 号:" ;
l_bookid.size = new system.drawing.size ( 142 , 20 ) ;
l_bookid.font = new system.drawing.font ( "宋体" , 12f ) ;
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 ( 142 , 20 ) ;
l_booktitle.font = new system.drawing.font ( "宋体" , 12f ) ;
l_booktitle.textalign = system.drawing.contentalignment.middlecenter ;
l_bookauthor.location = new system.drawing.point ( 24 , 160 ) ;
l_bookauthor.text = "作 者:";
l_bookauthor.size = new system.drawing.size ( 142 , 20 ) ;
l_bookauthor.font = new system.drawing.font ( "宋体" , 12f ) ;
l_bookauthor.textalign = system.drawing.contentalignment.middlecenter ;
l_bookprice.location = new system.drawing.point ( 24 , 212 ) ;
l_bookprice.text = "价 格:" ;
l_bookprice.size = new system.drawing.size ( 142 , 20 ) ;
l_bookprice.font = new system.drawing.font ( "宋体" , 12f ) ;
l_bookprice.textalign = system.drawing.contentalignment.middlecenter ;
l_bookstock.location = new system.drawing.point ( 24 , 264 ) ;
l_bookstock.text = "书 架 号:" ;
l_bookstock.size = new system.drawing.size ( 142 , 20 ) ;
l_bookstock.font = new system.drawing.font ( "宋体" , 12f ) ;
l_bookstock.textalign = system.drawing.contentalignment.middlecenter ;
//以下设定程序中用到的功能按钮的属性及对应的事件
delete.location = new system.drawing.point ( 104 , 352 ) ;
delete.forecolor = system.drawing.color.black ;
delete.size = new system.drawing.size ( 80 , 24 ) ;
delete.font = new system.drawing.font ( "宋体" , 9f ) ;
delete.text = "删除记录" ;
delete.click += new system.eventhandler ( godelete ) ;
update.location = new system.drawing.point ( 204 , 352 ) ;
update.forecolor = system.drawing.color.black ;
update.size = new system.drawing.size ( 80 , 24 ) ;
update.font = new system.drawing.font ( "宋体" , 9f ) ;
update.text = "修改记录" ;
update.click += new system.eventhandler ( goupdate ) ;
firstrec.location = new system.drawing.point ( 64 , 312 ) ;
firstrec.forecolor = system.drawing.color.black ;
firstrec.size = new system.drawing.size ( 40 , 24 ) ;
firstrec.font = new system.drawing.font ( "宋体" , 9f ) ;
firstrec.text = "首记录" ;
firstrec.click += new system.eventhandler ( gofirst ) ;
previousrec.location = new system.drawing.point ( 136 , 312 ) ;
previousrec.forecolor = system.drawing.color.black ;
previousrec.size = new system.drawing.size ( 40 , 24 ) ;
previousrec.font = new system.drawing.font ( "宋体" , 9f ) ;
previousrec.text = "上一条" ;
previousrec.click += new system.eventhandler ( goprevious ) ;
nextrec.location = new system.drawing.point ( 208 , 312 ) ;
nextrec.forecolor = system.drawing.color.black ;
nextrec.size = new system.drawing.size ( 40 , 24 ) ;
nextrec.font = new system.drawing.font ( "宋体" , 9f ) ;
nextrec.text = "下一条" ;
nextrec.click += new system.eventhandler ( gonext ) ;
lastrec.location = new system.drawing.point ( 280 , 312 ) ;
lastrec.forecolor = system.drawing.color.black ;
lastrec.size = new system.drawing.size ( 40 , 24 ) ;
lastrec.font = new system.drawing.font ( "宋体" , 9f ) ;
lastrec.text = "尾记录" ;
lastrec.click += new system.eventhandler ( golast ) ;
label1.location = new system.drawing.point ( 60 , 20 ) ;
label1.text = "用visual c#来修改和删除数据库中的记录" ;
label1.size = new system.drawing.size ( 296 , 24 ) ;
label1.forecolor = system.drawing.systemcolors.desktop ;
label1.font = new system.drawing.font ( "宋体" , 14f ) ;
//设定程序的主窗体的属性
this.text = "用visual c#来修改和删除数据库中的记录!" ;
this.autoscalebasesize = new system.drawing.size ( 5 , 13 ) ;
this.formborderstyle = formborderstyle.fixedsingle ;
this.clientsize = new system.drawing.size ( 394 , 425 ) ;
//在主窗体中加入组件
this.controls.add ( delete ) ;
this.controls.add ( update ) ;
this.controls.add ( lastrec ) ;
this.controls.add ( nextrec ) ;
this.controls.add ( previousrec ) ;
this.controls.add ( firstrec ) ;
this.controls.add ( t_bookstock ) ;
this.controls.add ( t_bookprice ) ;
this.controls.add ( t_bookauthor ) ;
this.controls.add ( t_booktitle ) ;
this.controls.add ( t_bookid ) ;
this.controls.add ( l_bookstock ) ;
this.controls.add ( l_bookprice ) ;
this.controls.add ( l_bookauthor ) ;
this.controls.add ( l_booktitle ) ;
this.controls.add ( l_bookid ) ;
this.controls.add ( label1 ) ;
}
//"删除记录"对应的事件
protected void godelete ( object sender, system.eventargs e )
{
try{
//连接到一个数据库
string strcon = " provider = microsoft.jet.oledb.4.0 ; data source = sample.mdb " ;
oledbconnection myconn = new oledbconnection ( strcon ) ;
myconn.open ( ) ;
string strdele = "delete from books where bookid= " + t_bookid.text ;
oledbcommand mycommand = new oledbcommand ( strdele , myconn ) ;
//从数据库中删除指定记录
mycommand.executenonquery ( ) ;
//从dataset中删除指定记录
mydataset.tables [ "books" ] . rows [ mybind.position ] . delete ( ) ;
mydataset.tables [ "books" ] . acceptchanges ( ) ;
myconn.close ( ) ;
}
catch ( exception ed )
{
messagebox.show ( "删除记录错误信息: " + ed.tostring ( ) , "错误!" ) ;
}
}
//"修改记录"按钮对应的事件
protected void goupdate ( object sender , system.eventargs e )
{
int i = mybind.position ;
try{
//连接到一个数据库
string strcon = " provider = microsoft.jet.oledb.4.0 ; data source = sample.mdb " ;
oledbconnection myconn = new oledbconnection ( strcon ) ;
myconn.open ( ) ;
//从数据库中修改指定记录
string strupdt = " update books set booktitle = "
+ t_booktitle.text + " , bookauthor = "
+ t_bookauthor.text + " , bookprice = "
+ t_bookprice.text + " , bookstock = "
+ t_bookstock.text + " where bookid = " + t_bookid.text ;
oledbcommand mycommand = new oledbcommand ( strupdt , myconn ) ;
mycommand.executenonquery ( ) ;
myconn.close ( ) ;
}
catch ( exception ed )
{
messagebox.show ( "修改指定记录错误: " + ed.tostring ( ) , "错误!" ) ;
}
mybind.position = i ;
}
//"尾记录"按钮对应的事件
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 ;
}
}
|