(1)创建并打开一个OleDbConnection对象。
(2)创建一个插入一条记录的SQL语句。
(3)创建一个OleDbCommand对象。
(4)通过此OleDbCommand对象完成对插入一条记录到数据库的操作。
以下是在程序中实现的具体语句:
string strConn = " Provider = Microsoft.Jet.OLEDB.4.0 Data Source = sample.mdb "
OleDbConnectionmyConn = new OleDbConnection ( strConn )
myConn.Open ( )
string strInsert = " INSERT INTO books ( bookid , booktitle , bookauthor , bookprice , bookstock )
VALUES ( "
strInsert += t_bookid.Text + ", '"
strInsert += t_booktitle.Text + "', '"
strInsert += t_bookauthor.Text + "', "
strInsert += t_bookprice.Text + ", "
strInsert += t_bookstock.Text + ")"
OleDbCommandinst = new OleDbCommand ( strInsert , myConn )
inst.ExecuteNonQuery ( )
myConn.Close ( )