C数据库编程添加删除记录.docx
- 文档编号:17308303
- 上传时间:2023-07-24
- 格式:DOCX
- 页数:18
- 大小:18.83KB
C数据库编程添加删除记录.docx
《C数据库编程添加删除记录.docx》由会员分享,可在线阅读,更多相关《C数据库编程添加删除记录.docx(18页珍藏版)》请在冰点文库上搜索。
C数据库编程添加删除记录
本文就接着上一篇的内容,来介绍用VisualC#如何来修改和删除数据记录。
一.程序设计和运行的环境设置
(1).视窗2000服务器版
(2).MicrosoftAccessDataComponent2.6以上版本(MADC2.6)
(3).本文程序使用的数据库的介绍:
为了方便起见,在选用数据库方面选用了本地数据库Access2000,当然你也可以选用其他类型的数据库,只需要更改文章后面的程序源代码中数据库的引擎,并更改对应的代码就可以了。
本程序中使用的数据库名称为sample.mdb,在此数据库中有一张数据表books。
此数据表的结构如下
字段名称字段类型代表意思
Bookid数字序号
booktitle文本书籍名称
bookauthor文本书籍作者
bookprice数字价格
bookstock数字书架号
二.程序设计难点和应该注意的问题
在程序设计中的重点和难点就是如何用VisualC#删除记录和如何修改记录。
下面就这二个问题进行必要的论述
(1).如何用VisualC#正确删除数据表中的记录
在用VisualC#删除记录的时候要注意的是:
必须从二个方面彻底删除记录,即从数据库和用VisualC#编程时产生的一个DataSet对象中彻底删除。
在程序设计的时候,如果只是删除了DataSet对象中的记录信息,这种删除是一种伪删除。
这是因为当他退出程序,又重新运行程序,会发现,那个要删除的记录依然还存在。
这是因为DataSet对象只是对数据表的一个镜像,并不是真正的记录本身。
但如果只是从数据库中删除记录,因为我们此时程序用到的数据集合是从DataSet对象中读取的,子DataSet对象中依然保存此条记录的镜像。
所以就会发现,我们根本没有删除掉记录,但实际上已经删除了。
此时只有退出程序,重新运行,才会发现记录已经删除了。
本文使用的方法是删除以上二个方面的记录或记录镜像信息。
当然你也可以使用其他的方法,譬如:
首先从数据库中删除记录,然后重新建立数据连接,重新创建一个新的DataSet对象。
这种方法虽然也可以达到相同目的,但显然相对繁杂些,所以本文采用的是第一种方法--直接删除。
在程序中具体的实现语句如下:
//连接到一个数据库
stringstrCon="Provider=Microsoft.Jet.OLEDB.4.0;DataSource=sample.mdb";
OleDbConnectionmyConn=newOleDbConnection(strCon);
myConn.Open();
stringstrDele="DELETEFROMbooksWHEREbookid="+t_bookid.Text;
OleDbCommandmyCommand=newOleDbCommand(strDele,myConn);
//从数据库中删除指定记录
myCommand.ExecuteNonQuery();
//从DataSet中删除指定记录信息
myDataSet.Tables["books"].Rows[myBind.Position].Delete();
myDataSet.Tables["books"].AcceptChanges();
myConn.Close();
(2).用VisualC#来修改数据表中的记录
在用VisualC#修改记录和删除记录,在程序设计中大致差不多,具体的实现方式也是通过SQL语句调用来实现的。
下面就是在程序中修改记录的具体语句:
//连接到一个数据库
stringstrCon="Provider=Microsoft.Jet.OLEDB.4.0;DataSource=sample.mdb";
OleDbConnectionmyConn=newOleDbConnection(strCon);
myConn.Open();
//从数据库中修改指定记录
stringstrUpdt="UPDATEbooksSETbooktitle='"
+t_booktitle.Text+"',bookauthor='"
+t_bookauthor.Text+"',bookprice="
+t_bookprice.Text+",bookstock="
+t_bookstock.Text+"WHEREbookid="+t_bookid.Text;
OleDbCommandmyCommand=newOleDbCommand(strUpdt,myConn);
myCommand.ExecuteNonQuery();
myConn.Close();
(3).在了解了如何用VisualC#删除和修改记录以后,结合《VisualC#中轻松浏览数据库记录》文的内容,就可以得到用VisualC#完成删除和修改数据记录的比较完整程序代码
三.用VisualC#实现删除和修改数据库记录的完整源程序代码
usingSystem;
usingSystem.Drawing;
usingSystem.ComponentModel;
usingSystem.Windows.Forms;
usingSystem.Data.OleDb;
usingSystem.Data;
publicclassDataEdit:
Form{privateSystem.ComponentModel.Containercomponents;
privateButtondelete;
privateButtonupdate;
privateButtonlastrec;
privateButtonnextrec;
privateButtonpreviousrec;
privateButtonfirstrec;
privateTextBoxt_bookstock;
privateTextBoxt_bookprice;
privateTextBoxt_bookauthor;
privateTextBoxt_booktitle;
privateTextBoxt_bookid;
privateLabell_bookstock;
privateLabell_bookprice;
privateLabell_bookauthor;
privateLabell_booktitle;
privateLabell_bookid;
privateLabellabel1;
privateSystem.Data.DataSetmyDataSet;
privateBindingManagerBasemyBind;
privateboolisBound=false;
//定义此变量,是判断组件是否已经绑定数据表中的字段
publicDataEdit(){
//对窗体中所需要的内容进行初始化
InitializeComponent();
//连接到一个数据库
GetConnected();
}
//清除程序中用到的所有资源
publicoverridevoidDispose(){
base.Dispose();
components.Dispose();
}
publicvoidGetConnected()
{
try{
//创建一个OleDbConnection对象
stringstrCon="Provider=Microsoft.Jet.OLEDB.4.0;DataSource=sample.mdb";
OleDbConnectionmyConn=newOleDbConnection(strCon);
stringstrCom="SELECT*FROMbooks";
//创建一个DataSet对象
myDataSet=newDataSet();
myConn.Open();
OleDbDataAdaptermyCommand=newOleDbDataAdapter(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(Exceptione)
{
MessageBox.Show("连接数据库发生错误为:
"+e.ToString(),"错误!
");
}
}
publicstaticvoidMain(){
Application.Run(newDataEdit());
}
privatevoidInitializeComponent()
{
ponents=newSystem.ComponentModel.Container();
this.t_bookid=newTextBox();
this.previousrec=newButton();
this.l_bookauthor=newLabel();
this.delete=newButton();
this.t_booktitle=newTextBox();
this.t_bookauthor=newTextBox();
this.t_bookprice=newTextBox();
this.l_bookprice=newLabel();
this.t_bookstock=newTextBox();
this.l_bookstock=newLabel();
this.l_booktitle=newLabel();
this.update=newButton();
this.nextrec=newButton();
this.lastrec=newButton();
this.firstrec=newButton();
this.label1=newLabel();
this.l_bookid=newLabel();
t_bookid.Location=newSystem.Drawing.Point(184,56);
t_bookid.Size=newSystem.Drawing.Size(80,20);
t_booktitle.Location=newSystem.Drawing.Point(184,108);
t_booktitle.Size=newSystem.Drawing.Size(176,20);
t_bookauthor.Location=newSystem.Drawing.Point(184,160);
t_bookauthor.Size=newSystem.Drawing.Size(128,20);
t_bookprice.Location=newSystem.Drawing.Point(184,212);
t_bookprice.Size=newSystem.Drawing.Size(80,20);
t_bookstock.Location=newSystem.Drawing.Point(184,264);
t_bookstock.Size=newSystem.Drawing.Size(80,20);
//以下是设定在程序中使用到的Label属性
l_bookid.Location=newSystem.Drawing.Point(24,56);
l_bookid.Text="序号:
";
l_bookid.Size=newSystem.Drawing.Size(142,20);
l_bookid.Font=newSystem.Drawing.Font("宋体",12f);
l_bookid.TextAlign=System.Drawing.ContentAlignment.MiddleCenter;
l_booktitle.Location=newSystem.Drawing.Point(24,108);
l_booktitle.Text="书名:
";
l_booktitle.Size=newSystem.Drawing.Size(142,20);
l_booktitle.Font=newSystem.Drawing.Font("宋体",12f);
l_booktitle.TextAlign=System.Drawing.ContentAlignment.MiddleCenter;
l_bookauthor.Location=newSystem.Drawing.Point(24,160);
l_bookauthor.Text="作者:
";
l_bookauthor.Size=newSystem.Drawing.Size(142,20);
l_bookauthor.Font=newSystem.Drawing.Font("宋体",12f);
l_bookauthor.TextAlign=System.Drawing.ContentAlignment.MiddleCenter;
l_bookprice.Location=newSystem.Drawing.Point(24,212);
l_bookprice.Text="价格:
";
l_bookprice.Size=newSystem.Drawing.Size(142,20);
l_bookprice.Font=newSystem.Drawing.Font("宋体",12f);
l_bookprice.TextAlign=System.Drawing.ContentAlignment.MiddleCenter;
l_bookstock.Location=newSystem.Drawing.Point(24,264);
l_bookstock.Text="书架号:
";
l_bookstock.Size=newSystem.Drawing.Size(142,20);
l_bookstock.Font=newSystem.Drawing.Font("宋体",12f);
l_bookstock.TextAlign=System.Drawing.ContentAlignment.MiddleCenter;
//以下设定程序中用到的功能按钮的属性及对应的事件
delete.Location=newSystem.Drawing.Point(104,352);
delete.ForeColor=System.Drawing.Color.Black;
delete.Size=newSystem.Drawing.Size(80,24);
delete.Font=newSystem.Drawing.Font("宋体",9f);
delete.Text="删除记录";
delete.Click+=newSystem.EventHandler(GoDelete);
update.Location=newSystem.Drawing.Point(204,352);
update.ForeColor=System.Drawing.Color.Black;
update.Size=newSystem.Drawing.Size(80,24);
update.Font=newSystem.Drawing.Font("宋体",9f);
update.Text="修改记录";
update.Click+=newSystem.EventHandler(GoUpdate);
firstrec.Location=newSystem.Drawing.Point(64,312);
firstrec.ForeColor=System.Drawing.Color.Black;
firstrec.Size=newSystem.Drawing.Size(40,24);
firstrec.Font=newSystem.Drawing.Font("宋体",9f);
firstrec.Text="首记录";
firstrec.Click+=newSystem.EventHandler(GoFirst);
previousrec.Location=newSystem.Drawing.Point(136,312);
previousrec.ForeColor=System.Drawing.Color.Black;
previousrec.Size=newSystem.Drawing.Size(40,24);
previousrec.Font=newSystem.Drawing.Font("宋体",9f);
previousrec.Text="上一条";
previousrec.Click+=newSystem.EventHandler(GoPrevious);
nextrec.Location=newSystem.Drawing.Point(208,312);
nextrec.ForeColor=System.Drawing.Color.Black;
nextrec.Size=newSystem.Drawing.Size(40,24);
nextrec.Font=newSystem.Drawing.Font("宋体",9f);
nextrec.Text="下一条";
nextrec.Click+=newSystem.EventHandler(GoNext);
lastrec.Location=newSystem.Drawing.Point(280,312);
lastrec.ForeColor=System.Drawing.Color.Black;
lastrec.Size=newSystem.Drawing.Size(40,24);
lastrec.Font=newSystem.Drawing.Font("宋体",9f);
lastrec.Text="尾记录";
lastrec.Click+=newSystem.EventHandler(GoLast);
label1.Location=newSystem.Drawing.Point(60,20);
label1.Text="用VisualC#来修改和删除数据库中的记录";
label1.Size=newSystem.Drawing.Size(296,24);
label1.ForeColor=System.Drawing.SystemColors.Deskt
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 数据库 编程 添加 删除 记录
![提示](https://static.bingdoc.com/images/bang_tan.gif)