电信主站 网通分站
购买流程 付款方式 常见问题 在线提问 续租服务 购物车
用户名: 密 码: 忘记密码?
首 页
域名注册
虚拟主机
双线主机
服务器租用
VPS主机
企业邮局
代理专区
客服中心
虚拟主机行业资讯 虚拟主机评测对比 互联网最新动态 技术学院 站长资讯 在线教程 网站运营
搜索优化 服务器 网络编程 图形图象 站长之家 网页制作 操作系统
冲浪宝典 软件教学 视频通信 办公软件 邮件系统 网络安全 认证考试
您当前位置:西部数码->资讯中心-> 在线教程-> 数据库
用VisualC#来修改和删除数据库记录-.NET教程,C#语言
作者:网友供稿 点击:52
  西部数码-全国虚拟主机10强!20余项虚拟主机管理功能,全国领先!第6代双线路虚拟主机,南北访问畅通无阻!虚拟主机可在线rar解压,自动数据恢复设置虚拟目录等.虚拟主机免费赠送访问统计,企业邮局.Cn域名注册10元/年,自助建站480元起,免费试用7天,满意再付款!P4主机租用799元/月.月付免压金!
文章页数:[1] 
在以前的一篇文章《visual c#中轻松浏览数据库记录》中,我们介绍了用visual c#如何把数据表中的字段值绑定到文本框的属性上和如何操作数据记录指针,随意浏览数据表中的记录。本文就接着上一篇的内容,来介绍用visual c#如何来修改和删除数据记录。

一.程序设计和运行的环境设置:
(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#给我们带来的数据操作的快感了。
文章整理:西部数码--专业提供域名注册虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!
相关主题
文章页数:[1] 
Google
热门文章
·数据库开发个人总结(ADO.NET小结)-.NET教程,数据库应用
·怎么由DataSet将数据导入Excel?-.NET教程,数据库应用
·动态创建SQL Server数据库、表、存储过程-ASP教程,数据库相关
·Win32环境下动态链接库(DLL)编程原理-.NET教程,数据库应用
·封装的ADO.NET对数据库操作经典类-.NET教程,数据库应用
·在DataGridView中获得DataGridViewCheckBoxColumn的状态-ASP教程,数据库相关
·DataGrid使用心得(附大量代码)-ASP教程,数据库相关
·用代码创建DataGrid的多链接及checkbox事件响应-.NET教程,数据库应用
·ADO.NET 的最佳实践技巧-.NET教程,数据库应用
·转载: 用纯ASP代码实现图片上传并存入数据库中

最新文章
·根据数据表中数据,生成Powerpoint幻灯片-ASP教程,数据库相关
·DataGrid中的按钮反选事件与NamingContainer(命名容器)-downmoon-ASP教程,数据库相关
·使用用VB处理MYSQL数据库中二进制数据问题-.NET教程,VB.Net语言
·关于DataGridView中如何接收处于编辑状态下的当前信息-ASP教程,数据库相关
·在DataGridView中获得DataGridViewCheckBoxColumn的状态-ASP教程,数据库相关
·.net下访问Access数据库需要注意的问题-.NET教程,Asp.Net开发
·ActiveMQ4.1+Spring2.0的POJO JMS方案(上)-.NET教程,数据库应用
·ASP.NET 2.0中直接将Access数据库导入到Excel文件中-.NET教程,Asp.Net开发
·NET(C#)连接各类数据库-集锦-.NET教程,C#语言
·ASP.NET2.0连接SQL Server数据库详解-.NET教程,Asp.Net开发


 
 


版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!

特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。
  打印  刷新  关闭
返回首页 |关于我们 | 联系我们 | 付款方式 | 创业联盟 | 虚拟主机 | 资讯中心 | 友情链接 | 网站地图

版权所有 西部数码(www.west263.com)
CopyRight (c) 2002~2006 west263.com all right reserved.
公司地址:四川成都市万和路90号天象大厦4楼 邮编:610031
电话总机:028-86262244 86263048 86263408 86263960 86264018 86267838
售前咨询:总机转201 202 203 204 206 208
售后服务:总机转211 212 213 214
财务咨询:总机转224 223 传真:028-86264041 财务QQ:点击发送消息给对方635483282
售前咨询QQ:点击发送消息给对方2182518 点击发送消息给对方241975952 点击发送消息给对方275026793 点击发送消息给对方408235859
售后服务QQ:点击发送消息给对方17708515 点击发送消息给对方307742704 点击发送消息给对方287976517 点击发送消息给对方363783715
《中华人民共和国增值电信业务经营许可证》编号:川B2-20030065号