0

我有一个数据库,其中有一个名为Schedule. 它看起来像这样:

Schedule
--------
ScheduleID (number)
BackupID   (number)
Repository (text)
Interval   (text)
Type       (text)

现在我正在尝试将值插入此表中,如下所示:

List<string> scheduleData = this.scheduler.getSchedule();
string repository = scheduleData[0];
string interval = scheduleData[1];
string type = scheduleData[2];

List<string> selectedBackup = this.backupForm.getSelectedUser();
string backupID = selectedBackup[0];

string connetionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\\db\\dbBackupUtility.mdb";
OleDbConnection conn = new OleDbConnection(connetionString);
conn.Open();
OleDbCommand sql = new OleDbCommand("INSERT INTO Schedule (BackupID, Repository, Interval, Type) VALUES (@BackupID, @Repository, @Interval, @Type)", conn);
sql.Parameters.AddWithValue("@BackupID", backupID);
sql.Parameters.AddWithValue("@Repository", repository);
sql.Parameters.AddWithValue("@Interval", interval);
sql.Parameters.AddWithValue("@Type", type);

sql.ExecuteNonQuery();

conn.Close();  

但是,每次程序执行此行时,我都会收到 OleDbException:

sql.ExecuteNonQuery();

我看不到有关异常的任何其他信息,但我尝试使用 VS 中的查询分析器手动插入值,这很奇怪。我已经用阅读器或适配器尝试了上述方法,但每次都会遇到同样的错误......

异常文本:

Message: "Syntax error in INSERT statement"
StackTrace =    "at System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr)
   at System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult)
   at System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult)
   at System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult)
   at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method)
   at System.Data.OleDb.OleDbCommand.ExecuteNonQuery()
   at WindowsFormsApplication1.BackupUtility.scheduler_VisibleChanged(Object sender, EventArgs e) in c:\Users\Teichler\Dropbox\VS_Projects\Projects\WindowsFormsApplication1\WindowsFormsApplication1\MainForm.cs:Line104."
Inner Exception: null
4

3 回答 3

3

IntervalJet/Access 的关键字,将您的命令更改为这个(用括号括起来的间隔):

OleDbCommand sql = new OleDbCommand("INSERT INTO Schedule (BackupID, Repository, [Interval], Type) VALUES (@BackupID, @Repository, @Interval, @Type)", conn);
于 2013-07-02T14:08:08.027 回答
0

出现此错误是因为您有一个值null作为要插入的参数传递。该值的表列不接受NULL.

调试你的代码。识别NULL变量并使其具有正确的数据类型。

利用 ?换了@。

于 2013-07-02T13:28:06.257 回答
0

可能是因为 Type 是 sql 中的关键字。尝试将您的陈述从

"INSERT INTO Schedule (BackupID, Repository, Interval, Type)

"INSERT INTO Schedule (BackupID, Repository, Interval, [Type])

在括号中键入

于 2013-07-02T14:05:10.300 回答