1.创建并打开一个OleDbConnection对象

string strConn = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = sample.mdb " ;
OleDbConnection myConn = new OleDbConnection ( strCon ) ;
myConn.Open ( ) ;

2.创建插入的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 + ")" ;

3.创建一个OleDbCommand对象
OleDbCommand inst = new OleDbCommand ( strInsert , myConn ) ;

4.使用OleDbCommand对象来插入数据
inst.ExecuteNonQuery ( ) ;

5.关闭OleDbConnection
myConn.Close ( ) ;

:使用access数据库时 需要在插入数据时标明要插入数据对应的字段名!

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-01-09
  • 2021-11-19
  • 2021-12-09
  • 2022-12-23
  • 2022-12-23
  • 2021-05-22
猜你喜欢
  • 2022-12-23
  • 2022-02-12
  • 2021-12-27
  • 2021-06-07
  • 2021-11-04
  • 2022-12-23
  • 2021-09-29
相关资源
相似解决方案