当前位置:Gxlcms > 数据库问题 > 使用C#向ACCESS中插入数据(仅供参考)

使用C#向ACCESS中插入数据(仅供参考)

时间:2021-07-01 10:21:17 帮助过:4人阅读

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数据库时 需要在插入数据时标明要插入数据对应的字段名!

使用C#向ACCESS中插入数据(仅供参考)

标签:

人气教程排行