用Access作为后台数据库支撑,书写一个C#写入记录的案例

要想操作一个数据库,不论是那种操作,首先要做的肯定是打开数据库。

 下面我们以ACCESS数据库来做例子说明如何打开一个数据库连接!

    在这里我们需要用到的是:

    System.Data.OleDb.OleDbConnection类!(如果操作SQL数据库,我们最好使用System.Data.SqlClient.SqlConnection类)

使用程序:

 

我们先定义一个String类型的变量,其中存放了我们连接数据库的连接字符串,然后在定义一个System.Data.OleDb.OleDbConnection类型的对象并实例化,最后返回这个对象!

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 ( ) ;

posted @   格笙  阅读(368)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示