使用C#向ACCESS中插入数据(仅供参考)
2016-03-09 09:08
411 查看
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数据库时 需要在插入数据时标明要插入数据对应的字段名!
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#进行Visio二次开发之文件导出及另存Web页面
- C#用正则表达式 获取网页源代码标签的属性或值
- C# 分支语句
- MSDN知识库_c#关键字_static
- C# delegate多种变体
- 用C#实现Base64处理,加密解密,编码解码
- C# 必看书籍
- C#脚步控制角色在屏幕上旋转
- 以Access为支撑,书写一个C#写入记录的案例
- 转载:C#之接口简介
- 【C#进阶系列】07 常量和字段
- 在C#中创建、读、写EXCEL文件(基于COM)
- 转载:浅析C#深拷贝与浅拷贝
- C#成魔之路<10>文件及数据流技术(3)
- C#语言基础— switch case和if的嵌套练习
- C# chart 属性
- C#中StreamReader读取中文出现乱码
- C# 5.0 aynsc 和 await ,Trace改进
- C#图片按比例缩放
- C#索引器3 重载