okprice.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 v