我将在 C# 中使用 OleDbDataAdapter 在 Access 数据库中插入数据,但在 INSERT INTO 命令中出现消息语法错误的错误
BackgroundWorker worker = new BackgroundWorker();
OleDbDataAdapter dbAdapter new OleDbDataAdapter();
OleDbConnection dbConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\\PMS.mdb");
worker = new BackgroundWorker();
worker.WorkerReportsProgress = true;
worker.DoWork += InsertJob;
worker.ProgressChanged += InsertJobCompleted;
worker.RunWorkerAsync(args);
而 InsertJob 函数是:
private void InsertJob(object sender, DoWorkEventArgs e)
{
var args = (InsertJobArgs)e.Argument;
try
{
dbAdapter.SelectCommand = new OleDbCommand("SELECT * FROM Sheet", dbConnection);
dbAdapter.Fill(args.DataTable);
var builder = new OleDbCommandBuilder(dbAdapter);
var row = args.DataTable.NewRow();
row["UserName"] = args.Entry.UserName;
row["Password"] = args.Entry.Password;
args.DataTable.Rows.Add(row);
dbAdapter.InsertCommand = builder.GetInsertCommand();
dbAdapter.Update(args.DataTable);
builder.Dispose();
}
catch (Exception ex)
{
args.Exception = ex;
worker.ReportProgress(0, args);
return;
}
worker.ReportProgress(100, args);
}
我在线收到错误:dbAdapter.Update(args.DataTable);
我尝试用 Visual Studio 调试它,发现所有 InsertCommand 参数值都是null
我尝试在调用之前通过此代码手动插入它dbAdapter.Update(args.DataTable);
dbAdapter.InsertCommand.Parameters[0].Value = args.Entry.UserName;
dbAdapter.InsertCommand.Parameters[1].Value = args.Entry.Password;