首页 > 开发 > 综合 > 正文

用Visual C#来修改和删除数据库记录

2024-07-21 02:22:30
字体:
来源:转载
供稿:网友
中国最大的web开发资源网站及技术社区,
一.程序设计和运行的环境设置:
(1).视窗2000服务器版
(2).microsoft access data component 2.6 以上版本 ( madc 2.6 )
(3).本文程序使用的数据库的介绍:

为了方便起见,在选用数据库方面选用了本地数据库access 2000,当然你也可以选用其他类型的数据库,只需要更改文章后面的程序源代码中数据库的引擎,并更改对应的代码就可以了。本程序中使用的数据库名称为sample.mdb,在此数据库中有一张数据表books。此数据表的结构如下:
字段名称字段类型代表意思bookid数字序号booktitle文本书籍名称bookauthor文本书籍作者bookprice数字价格bookstock数字书架号
二.程序设计难点和应该注意的问题:
在程序设计中的重点和难点就是如何用visual c#删除记录和如何修改记录。下面就这二个问题进行必要的论述:
(1).如何用visual c#正确删除数据表中的记录:

在用visual c#删除记录的时候要注意的是:必须从二个方面彻底删除记录,即从数据库和用visual c#编程时产生的一个dataset对象中彻底删除。在程序设计的时候,如果只是删除了dataset对象中的记录信息,这种删除是一种伪删除。这是因为当他退出程序,又重新运行程序,会发现,那个要删除的记录依然还存在。这是因为dataset对象只是对数据表的一个镜像,并不是真正的记录本身。但如果只是从数据库中删除记录,因为我们此时程序用到的数据集合是从dataset对象中读取的,子dataset对象中依然保存此条记录的镜像。所以就会发现,我们根本没有删除掉记录,但实际上已经删除了。此时只有退出程序,重新运行,才会发现记录已经删除了。本文使用的方法是删除以上二个方面的记录或记录镜像信息。当然你也可以使用其他的方法,譬如:首先从数据库中删除记录,然后重新建立数据连接,重新创建一个新的dataset对象。这种方法虽然也可以达到相同目的,但显然相对繁杂些,所以本文采用的是第一种方法--直接删除。在程序中具体的实现语句如下:
//连接到一个数据库
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 ( ) ;

(2).用visual c#来修改数据表中的记录:
在用visual c#修改记录和删除记录,在程序设计中大致差不多,具体的实现方式也是通过sql语句调用来实现的。下面就是在程序中修改记录的具体语句:
//连接到一个数据库
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 ( ) ;


(3).在了解了如何用visual c#删除和修改记录以后,结合《visual c#中轻松浏览数据库记录》文的内容,就可以得到用visual c#完成删除和修改数据记录的比较完整程序代码,以下是本文中介绍程序的运行后的程序界面:

点击小图放大,本文中程序运行后的界面
三.用visual c#实现删除和修改数据库记录的完整源程序代码:
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 ;
}
}

四.编译源程序代码,生成执行文件:
得到了data.cs源程序代码以后,经过下面编译命令编译成功后,即可得到执行文件data.exe:
csc /t:winexe /r:system.windows.forms.dll /r:system.data.dll data.cs
五.总结:
本文主要介绍了如何用visual c#来删除和修改记录。以及在处理这些操作的时候,应该注意的一些重要问题及处理的方法。如果你的机器已经满足本文要求的运行环境,那么在生成的执行文件目录中建立一个sample.mdb数据库,在数据库中创建一个books数据表,数据表的结构可按照本文上面提供的结构来建立,在这一切都完毕后,就可以运行程序,享受visual c#给我们带来的数据操作的快感了。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表