0

当我尝试使用 更新 MS Access 数据库(.mdb文件)OleDbConnection时,出现此错误:

Microsoft JET 数据库引擎

在 System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr)
在 System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult)
在 System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult)
在 System.Data。在 System.Data.OleDb.OleDbCommand.ExecuteReaderInternal( CommandBehavior
行为,字符串方法)
在 System.Data.OleDb.OleDbCommand.ExecuteNonQuery()
在 CostEstimate.Form1.processdbBtn_Click(Object sender , EventArgs e) 在 C:\Users\sy\Visual Studio 2008\Projects\demo\demo\Form1.cs 中:第 137 行

第 137 行指command.ExecuteNonQuery();的是下面的代码

请在下面找到代码并帮助我解决问题

  OleDbConnection conn = new OleDbConnection();
  conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" + @"data source=C:\Users\sy\Visual Studio 2008\Projects\demo\demo\CE_Database.mdb;";

  conn.Open();

  OleDbCommand command = new OleDbCommand();
  string[][] allData = [[1,'a'],[2,'b'],[3,'c']]

  foreach (string[] individualData in allData)
  {
    command.CommandText = @"insert into CETable(JobCode,JobName) Values(individualData[0],individualData[1])";
    command.Connection = conn;
    command.ExecuteNonQuery();
  }

  conn.Close();
4

1 回答 1

0

尝试这个:

OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" + @"data source=C:\Users\sy\Visual Studio 2008\Projects\demo\demo\CE_Database.mdb;";
conn.Open();
string[][] allData = [[1,'a'],[2,'b'],[3,'c']]

foreach (string[] individualData in allData)
{
    OleDbCommand command = new OleDbCommand()
    {
        Connection = conn,
        CommandText = string.Format(@"insert into CETable(JobCode,JobName) Values({0}, {1});", individualData[0], individualData[1])";
    };
    command.ExecuteNonQuery();
}
conn.Close();

但我也建议您通过参数设置每个字段值。这是确保不注射的更好做法

于 2013-04-26T04:17:44.693 回答