具体的步骤:
1.创建并打开一个OleDbConnection对象
2.创建插入的SQL语句
3.创建一个OleDbCommand对象
4.使用OleDbCommand对象来插入数据
5.关闭OleDbConnection
实现的代码如下:
<在这里我们需要用到的是:System.Data.OleDb.OleDbConnection类!(如果操作SQL数据库,我们最好使用System.Data.SqlClient.SqlConnection类)>
using System.Data
using System.Data.OleDb
string strConn = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = sample.mdb " ;
OleDbConnection myConn = new OleDbConnection ( strConn ) ;
myConn.Open ( ) ;
SQL语句
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 + ")" ;
定义command对象,并执行相应的SQL语句
OleDbCommand inst = new OleDbCommand ( strInsert , myConn ) ;
inst.ExecuteNonQuery ( ) ;
myConn.Close ( ) ;