2013-07-02 21 views
0

我有一個數據庫,其中有一個名爲Schedule的表。它看起來像這樣:在.mdb數據庫中執行INSERT時OleDbException

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(); 

Howevery,我得到一個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 
+2

什麼是異常文本? –

+0

我無法找到有關異常的更多信息,VS只是告訴我System.Data.dll中發生OleDbException。該信息顯示在輸出窗口中,程序執行正常進行。 – LeonidasFett

+1

做一件事,在try - catch bock中粘貼上面的代碼。正確捕捉異常並讓我知道異常文本 –

回答

2

Intervalkeyword for Jet/Access,改變你的命令到這個(環繞間隔帶支架):

OleDbCommand sql = new OleDbCommand("INSERT INTO Schedule (BackupID, Repository, [Interval], Type) VALUES (@BackupID, @Repository, @Interval, @Type)", conn); 
+0

老兄,做到了!謝謝一堆! – LeonidasFett

0

此錯誤是因爲正在添加你有一個值null你是路過作爲插入的參數。您的表格列的值不接受NULL

調試你的代碼。識別NULL變量並將其設置爲適當的數據類型。

使用? instaed @。

+0

好的我再次調試,但沒有發現空值。所有4個參數都有值:backupID有4個,存儲庫有「Daily」,間隔有「03:00 PM」,類型有「Full」 – LeonidasFett

+0

使用? instaed @ –

+0

我也試過這種方式,也用OleDbCommand.AddWithValue方法。可悲的是,兩個都不工作... – LeonidasFett

0

其可能是因爲Type是sql中的一個關鍵字。嘗試從

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

改變你的語句來

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

周圍型括號

+0

對不起,但這也沒有解決錯誤... – LeonidasFett