日期:2014-05-19  浏览次数:21010 次

在c sharp程序中如何在调用的access表格中插入一行
例如:
...调用了一个access文件欲对其中一张表(TableName)内插入一行三列元素分别为item1\2\3,设定第一列item0为自动添加序列号项,如下
...
string   insertString   = "INSERT   INTO   TableName(item1,item2,item3) "+ "VALUES   ( 'FileLocation ',   10,100 "   + ") ";

System.Data.OleDb.OleDbCommand   oledbCommand=new   System.Data.OleDb.OleDbCommand(insertString,m_oledbConnection);
oledbCommand.CommandType=   System.Data.CommandType.Text;
System.Data.OleDb.OleDbDataReader   rowreader=oledbCommand.ExecuteReader();
rowreader.Close();
...

调试发现程序在倒数第二行出现错误
哪位高手再帮忙解决下,谢谢
提示:我直接在sql里执行 "INSERT   INTO   TableName(item1,item2,item3) "+ "VALUES   ( 'FileLocation ',   10,100 "   + ") ";可以对sql里的表格进行行的插入,没有问题

如有不明之处请跟贴,我会详细说明,谢谢!



------解决方案--------------------
我直接在sql里执行 "INSERT INTO TableName(item1,item2,item3) "+ "VALUES ( 'FileLocation ', 10,100 " + ") ";可以对sql里的表格进行行的插入,没有问题

========

这句要是可以执行,就真乞丐了

注意 10,100 中你用一个全角的 , 逗号

> >

"INSERT INTO TableName(item1,item2,item3) "+ "VALUES ( 'FileLocation ', 10, 100 " + ") ";
------解决方案--------------------
下次,请注明详细的错误信息
------解决方案--------------------
如果问题是输入法的问题,也就是语法问题,是不能通过编译的。既然第一列设置了自动列,为什么还要再手动写数据呢?
------解决方案--------------------
m_oledbConnection.open();
参考一下这里http://www.dbforums.com/archive/index.php/t-482552.html
------解决方案--------------------
http://support.microsoft.com/kb/313547/zh-cn?spid=548&sid=883这里有解答。。看不太懂
微软的帮助就是这个样子。。郁闷
------解决方案--------------------
string con =@ " Provider=Microsoft.Jet.OLEDB.4.0;Data Source= ";
string dbname=@ "\ElectronMap.dat ";
try
{
this.oleDbConnection1.Close();
this.oleDbConnection1.ConnectionString = con + Directory.GetCurrentDirectory() + dbname;
this.oleDbConnection1.Open();

this.oleDbDataAdapter1.SelectCommand.CommandText = "select * from AlarmType ";
this.oleDbDataAdapter1.SelectCommand.ExecuteNonQuery();
this.oleDbDataAdapter1.Fill(this.dataSet1);
this.dataGridView1.DataSource = this.dataSet1.Tables[0];

this.oleDbDataAdapter1.InsertCommand.CommandText = "insert into AlarmType values(@aa,@bb,@cc) ";
this.oleDbDataAdapter1.InsertCommand.Parameters.Clear();
this.oleDbDataAdapter1.InsertCommand.Parameters.AddWithValue( "@aa ", "摄像头 ");
this.oleDbDataAdapter1.InsertCommand.Parameters.AddWithValue( "@bb ", this.dataSet1.Tables[0].Rows.Count+1);
this.oleDbDataAdapter1.InsertCommand.Parameters.AddWithValue( "@cc ", "是的 ");
this.oleDbDataAdapter1.InsertCommand.ExecuteNonQuery();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
if (this.oleDbConnection1.State == ConnectionState.Open)
{
this.oleDbConnection1.Close();