在以前的一篇文章《<ccid_nobr><a href="http://developer.ccidnet.com/pub/disp/Article?columnID=295&articleID=9758&pageNO=1" target="_blank"><font color="#002c99">Visual C#中轻松浏览数据库记录</font></a></ccid_nobr>》中,我们介绍了用Visual C#如何把数据表中的字段值绑定到文本框的属性上和如何操作数据记录指针,随意浏览数据表中的记录。本文就接着上一篇的内容,来介绍用Visual C#如何来修改和删除数据记录。
<ccid_nobr><b>一.程序设计和运行的环境设置:</b></ccid_nobr>
<ccid_nobr></ccid_nobr>
字段名称 | 字段类型 | 代表意思 |
Bookid | 数字 | 序号 |
booktitle | 文本 | 书籍名称 |
bookauthor | 文本 | 书籍作者 |
bookprice | 数字 | 价格 |
bookstock | 数字 | 书架号 |
<ccid_nobr><b>二.程序设计难点和应该注意的问题:</b></ccid_nobr>
在程序设计中的重点和难点就是如何用Visual C#删除记录和如何修改记录。下面就这二个问题进行必要的论述:
<ccid_nobr></ccid_nobr>
//连接到一个数据库 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 ( ) ; |
//连接到一个数据库 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 ( ) ; |
<ccid_nobr><b>三.用Visual C#实现删除和修改数据库记录的完整源程序代码:</b></ccid_nobr>
<ccid_nobr><table cellspacing="0" bordercolordark="#ffffff" cellpadding="0" width="550" bordercolorlight="black" border="1"><tbody><tr><td class="code" bgcolor="#e6e6e6">using System ;<br>using System.Drawing ;<br>using System.ComponentModel ;<br>using System.Windows.Forms ;<br>using System.Data.OleDb ;<br>using System.Data ;<br>public class DataEdit : Form { private System.ComponentModel.Container components ;<br>private Button delete ;<br>private Button update ;<br>private Button lastrec ;<br>private Button nextrec ;<br>private Button previousrec ;<br>private Button firstrec ;<br>private TextBox t_bookstock ;<br>private TextBox t_bookprice ;<br>private TextBox t_bookauthor ;<br>private TextBox t_booktitle ;<br>private TextBox t_bookid ;<br>private Label l_bookstock ;<br>private Label l_bookprice ;<br>private Label l_bookauthor ;<br>private Label l_booktitle ;<br>private Label l_bookid ;<br>private Label label1 ;<br>private System.Data.DataSet myDataSet ;<br>private BindingManagerBase myBind ;<br>private bool isBound = false ;<br>//定义此变量,是判断组件是否已经绑定数据表中的字段<br><br>public DataEdit ( ) {<br>// 对窗体中所需要的内容进行初始化<br>InitializeComponent ( ) ;<br>//连接到一个数据库<br>GetConnected ( ) ;<br>}<br>//清除程序中用到的所有资源<br>public override void Dispose ( ) {<br>base.Dispose ( ) ;<br>components.Dispose ( ) ;<br>}<br>public void GetConnected ( )<br>{<br>try{<br>//创建一个 OleDbConnection对象<br>string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = sample.mdb " ;<br>OleDbConnection myConn = new OleDbConnection ( strCon ) ;<br>string strCom = " SELECT * FROM books " ;<br>//创建一个 DataSet对象<br>myDataSet = new DataSet ( ) ;<br>myConn.Open ( ) ;<br>OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom , myConn ) ;<br>myCommand.Fill ( myDataSet , "books" ) ;<br>myConn.Close ( ) ;<br>//判断数据字段是否绑定到 TextBoxes<br>if ( !isBound )<br>{<br>//以下是为显示数据记录而把数据表的某个字段绑定在不同的绑定到文本框"Text"属性上<br>t_bookid.DataBindings.Add ( "Text" , myDataSet , "books.bookid" ) ;<br>t_booktitle.DataBindings.Add ( "Text" , myDataSet , "books.booktitle" ) ;<br>t_bookauthor.DataBindings.Add ( "Text" , myDataSet , "books.bookauthor" ) ;<br>t_bookprice.DataBindings.Add ( "Text" , myDataSet , "books.bookprice" ) ;<br>t_bookstock.DataBindings.Add ( "Text" , myDataSet , "books.bookstock" ) ;<br>//设定 BindingManagerBase<br>//把对象DataSet和"books"数据表绑定到此myBind对象<br>myBind = this.BindingContext [ myDataSet , "books" ] ;<br>isBound = true ;<br>}<br>}<br>catch ( Exception e )<br>{<br>MessageBox.Show ( "连接数据库发生错误为:" + e.ToString ( ) , "错误!" ) ;<br>}<br>}<br>public static void Main ( ) {<br>Application.Run ( new DataEdit ( ) ) ;<br>}<br>private void InitializeComponent ( )<br>{<br>this.components = new System.ComponentModel.Container ( ) ;<br>this.t_bookid = new TextBox ( ) ;<br>this.previousrec = new Button ( ) ;<br>this.l_bookauthor = new Label ( ) ;<br>this.delete = new Button ( ) ;<br>this.t_booktitle = new TextBox ( ) ;<br>this.t_bookauthor = new TextBox ( ) ;<br>this.t_bookprice = new TextBox ( ) ;<br>this.l_bookprice = new Label ( ) ;<br>this.t_bookstock = new TextBox ( ) ;<br>this.l_bookstock = new Label ( ) ;<br>this.l_booktitle = new Label ( ) ;<br>this.update = new Button ( ) ;<br>this.nextrec = new Button ( ) ;<br>this.lastrec = new Button ( ) ;<br>this.firstrec = new Button ( ) ;<br>this.label1 = new Label ( ) ;<br>this.l_bookid = new Label ( ) ;<br>t_bookid.Location = new System.Drawing.Point ( 184 , 56 ) ;<br>t_bookid.Size = new System.Drawing.Size ( 80 , 20 ) ;<br><br>t_booktitle.Location = new System.Drawing.Point ( 184 , 108 ) ;<br>t_booktitle.Size = new System.Drawing.Size ( 176 , 20 ) ;<br><br>t_bookauthor.Location = new System.Drawing.Point ( 184 , 160 ) ;<br>t_bookauthor.Size = new System.Drawing.Size ( 128 , 20 ) ;<br><br>t_bookprice.Location = new System.Drawing.Point ( 184 , 212 ) ;<br>t_bookprice.Size = new System.Drawing.Size ( 80 , 20 ) ;<br><br>t_bookstock.Location = new System.Drawing.Point ( 184 , 264 ) ;<br>t_bookstock.Size = new System.Drawing.Size ( 80 , 20 ) ;<br>//以下是设定在程序中使用到的Label属性<br>l_bookid.Location = new System.Drawing.Point ( 24 , 56 ) ;<br>l_bookid.Text = "序 号:" ;<br>l_bookid.Size = new System.Drawing.Size ( 142 , 20 ) ;<br>l_bookid.Font = new System.Drawing.Font ( "宋体" , 12f ) ;<br>l_bookid.TextAlign = System.Drawing.ContentAlignment.MiddleCenter ;<br>l_booktitle.Location = new System.Drawing.Point ( 24 , 108 ) ;<br>l_booktitle.Text = "书 名:" ;<br>l_booktitle.Size = new System.Drawing.Size ( 142 , 20 ) ;<br>l_booktitle.Font = new System.Drawing.Font ( "宋体" , 12f ) ;<br>l_booktitle.TextAlign = System.Drawing.ContentAlignment.MiddleCenter ;<br>l_bookauthor.Location = new System.Drawing.Point ( 24 , 160 ) ;<br>l_bookauthor.Text = "作 者:";<br>l_bookauthor.Size = new System.Drawing.Size ( 142 , 20 ) ;<br>l_bookauthor.Font = new System.Drawing.Font ( "宋体" , 12f ) ;<br>l_bookauthor.TextAlign = System.Drawing.ContentAlignment.MiddleCenter ;<br>l_bookprice.Location = new System.Drawing.Point ( 24 , 212 ) ;<br>l_bookprice.Text = "价 格:" ;<br>l_bookprice.Size = new System.Drawing.Size ( 142 , 20 ) ;<br>l_bookprice.Font = new System.Drawing.Font ( "宋体" , 12f ) ;<br>l_bookprice.TextAlign = System.Drawing.ContentAlignment.MiddleCenter ;<br><br>l_bookstock.Location = new System.Drawing.Point ( 24 , 264 ) ;<br>l_bookstock.Text = "书 架 号:" ;<br>l_bookstock.Size = new System.Drawing.Size ( 142 , 20 ) ;<br>l_bookstock.Font = new System.Drawing.Font ( "宋体" , 12f ) ;<br>l_bookstock.TextAlign = System.Drawing.ContentAlignment.MiddleCenter ;<br><br>//以下设定程序中用到的功能按钮的属性及对应的事件<br>delete.Location = new System.Drawing.Point ( 104 , 352 ) ;<br>delete.ForeColor = System.Drawing.Color.Black ;<br>delete.Size = new System.Drawing.Size ( 80 , 24 ) ;<br>delete.Font = new System.Drawing.Font ( "宋体" , 9f ) ;<br>delete.Text = "删除记录" ;<br>delete.Click += new System.EventHandler ( GoDelete ) ;<br><br>update.Location = new System.Drawing.Point ( 204 , 352 ) ;<br>update.ForeColor = System.Drawing.Color.Black ;<br>update.Size = new System.Drawing.Size ( 80 , 24 ) ;<br>update.Font = new System.Drawing.Font ( "宋体" , 9f ) ;<br>update.Text = "修改记录" ;<br>update.Click += new System.EventHandler ( GoUpdate ) ;<br><br>firstrec.Location = new System.Drawing.Point ( 64 , 312 ) ;<br>firstrec.ForeColor = System.Drawing.Color.Black ;<br>firstrec.Size = new System.Drawing.Size ( 40 , 24 ) ;<br>firstrec.Font = new System.Drawing.Font ( "宋体" , 9f ) ;<br>firstrec.Text = "首记录" ;<br>firstrec.Click += new System.EventHandler ( GoFirst ) ;<br><br>previousrec.Location = new System.Drawing.Point ( 136 , 312 ) ;<br>previousrec.ForeColor = System.Drawing.Color.Black ;<br>previousrec.Size = new System.Drawing.Size ( 40 , 24 ) ;<br>previousrec.Font = new System.Drawing.Font ( "宋体" , 9f ) ;<br>previousrec.Text = "上一条" ;<br>previousrec.Click += new System.EventHandler ( GoPrevious ) ;<br><br>nextrec.Location = new System.Drawing.Point ( 208 , 312 ) ;<br>nextrec.ForeColor = System.Drawing.Color.Black ;<br>nextrec.Size = new System.Drawing.Size ( 40 , 24 ) ;<br>nextrec.Font = new System.Drawing.Font ( "宋体" , 9f ) ;<br>nextrec.Text = "下一条" ;<br>nextrec.Click += new System.EventHandler ( GoNext ) ;<br><br>lastrec.Location = new System.Drawing.Point ( 280 , 312 ) ;<br>lastrec.ForeColor = System.Drawing.Color.Black ;<br>lastrec.Size = new System.Drawing.Size ( 40 , 24 ) ;<br>lastrec.Font = new System.Drawing.Font ( "宋体" , 9f ) ;<br>lastrec.Text = "尾记录" ;<br>lastrec.Click += new System.EventHandler ( GoLast ) ;<br><br>label1.Location = new System.Drawing.Point ( 60 , 20 ) ;<br>label1.Text = "用Visual C#来修改和删除数据库中的记录" ;<br>label1.Size = new System.Drawing.Size ( 296 , 24 ) ;<br>label1.ForeColor = System.Drawing.SystemColors.Desktop ;<br>label1.Font = new System.Drawing.Font ( "宋体" , 14f ) ;<br>//设定程序的主窗体的属性<br>this.Text = "用Visual C#来修改和删除数据库中的记录!" ;<br>this.AutoScaleBaseSize = new System.Drawing.Size ( 5 , 13 ) ;<br>this.FormBorderStyle = FormBorderStyle.FixedSingle ;<br>this.ClientSize = new System.Drawing.Size ( 394 , 425 ) ;<br>//在主窗体中加入组件<br>this.Controls.Add ( delete ) ;<br>this.Controls.Add ( update ) ;<br>this.Controls.Add ( lastrec ) ;<br>this.Controls.Add ( nextrec ) ;<br>this.Controls.Add ( previousrec ) ;<br>this.Controls.Add ( firstrec ) ;<br>this.Controls.Add ( t_bookstock ) ;<br>this.Controls.Add ( t_bookprice ) ;<br>this.Controls.Add ( t_bookauthor ) ;<br>this.Controls.Add ( t_booktitle ) ;<br>this.Controls.Add ( t_bookid ) ;<br>this.Controls.Add ( l_bookstock ) ;<br>this.Controls.Add ( l_bookprice ) ;<br>this.Controls.Add ( l_bookauthor ) ;<br>this.Controls.Add ( l_booktitle ) ;<br>this.Controls.Add ( l_bookid ) ;<br>this.Controls.Add ( label1 ) ;<br><br>}<br>//"删除记录"对应的事件<br>protected void GoDelete ( object sender, System.EventArgs e )<br>{<br>try{<br>//连接到一个数据库<br>string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = sample.mdb " ;<br>OleDbConnection myConn = new OleDbConnection ( strCon ) ;<br>myConn.Open ( ) ;<br>string strDele = "DELETE FROM books WHERE bookid= " + t_bookid.Text ;<br>OleDbCommand myCommand = new OleDbCommand ( strDele , myConn ) ;<br>//从数据库中删除指定记录<br>myCommand.ExecuteNonQuery ( ) ;<br>//从DataSet中删除指定记录<br>myDataSet.Tables [ "books" ] . Rows [ myBind.Position ] . Delete ( ) ;<br>myDataSet.Tables [ "books" ] . AcceptChanges ( ) ;<br>myConn.Close ( ) ;<br>}<br>catch ( Exception ed )<br>{<br>MessageBox.Show ( "删除记录错误信息: " + ed.ToString ( ) , "错误!" ) ;<br>}<br>}<br>//"修改记录"按钮对应的事件<br>protected void GoUpdate ( object sender , System.EventArgs e )<br>{<br>int i = myBind.Position ;<br>try{<br>//连接到一个数据库<br>string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = sample.mdb " ;<br>OleDbConnection myConn = new OleDbConnection ( strCon ) ;<br>myConn.Open ( ) ;<br><br>//从数据库中修改指定记录<br>string strUpdt = " UPDATE books SET booktitle = '"<br>+ t_booktitle.Text + "' , bookauthor = '"<br>+ t_bookauthor.Text + "' , bookprice = "<br>+ t_bookprice.Text + " , bookstock = "<br>+ t_bookstock.Text + " WHERE bookid = " + t_bookid.Text ;<br><br>OleDbCommand myCommand = new OleDbCommand ( strUpdt , myConn ) ;<br>myCommand.ExecuteNonQuery ( ) ;<br>myConn.Close ( ) ;<br>}<br>catch ( Exception ed )<br>{<br>MessageBox.Show ( "修改指定记录错误: " + ed.ToString ( ) , "错误!" ) ;<br>}<br>myBind.Position = i ;<br>}<br>//"尾记录"按钮对应的事件<br>protected void GoLast ( object sender , System.EventArgs e )<br>{<br>myBind.Position = myBind.Count - 1 ;<br>}<br>//"下一条"按钮对应的事件<br>protected void GoNext ( object sender , System.EventArgs e )<br>{<br>if ( myBind.Position == myBind.Count - 1 )<br>MessageBox.Show ( "已经到尾记录!" ) ;<br>else<br>myBind.Position += 1 ;<br>}<br>//"上一条"按钮对应的事件<br>protected void GoPrevious ( object sender , System.EventArgs e )<br>{<br>if ( myBind.Position == 0 )<br>MessageBox.Show ( "已经到首记录!" ) ;<br>else<br>myBind.Position -= 1 ;<br>}<br>//"首记录"按钮对应的事件<br>protected void GoFirst ( object sender , System.EventArgs e )<br>{<br>myBind.Position = 0 ;<br>}<br>}<br> </td></tr></tbody></table> <ccid_nobr><b>四.编译源程序代码,生成执行文件:</b></ccid_nobr><br><br><br>得到了Data.cs源程序代码以后,经过下面编译命令编译成功后,即可得到执行文件data.exe: <br><br><table cellspacing="0" bordercolordark="#ffffff" cellpadding="0" width="550" bordercolorlight="black" border="1"><tbody><tr><td class="code" bgcolor="#e6e6e6">csc /t:winexe /r:system.windows.forms.dll /r:system.data.dll data.cs</td></tr></tbody></table> <br><br></ccid_nobr>
<ccid_nobr><b>五.总结:</b></ccid_nobr>
本文主要介绍了如何用Visual C#来删除和修改记录。以及在处理这些操作的时候,应该注意的一些重要问题及处理的方法。如果你的机器已经满足本文要求的运行环境,那么在生成的执行文件目录中建立一个sample.mdb数据库,在数据库中创建一个books数据表,数据表的结构可按照本文上面提供的结构来建立,在这一切都完毕后,就可以运行程序,享受Visual C#给我们带来的数据操作的快感了。