2014-02-05 50 views
0

我想在C#中的現有MS Access數據庫表中添加一個新行。但是,當我調用DataAdapter的Update方法時,出現了「INSERT INTO語句中的語法錯誤」。我的代碼是:當我調用DataAdapter.Update()時,爲什麼此C#代碼會生成語法錯誤?

try 
{ 
    // Open the database 
    OleDbCommand mySelectCommand = new OleDbCommand(strAccessSelect, myAccessConn); 
    OleDbDataAdapter myDataAdapter = new OleDbDataAdapter(mySelectCommand); 
    OleDbCommandBuilder myCommandBuilder = new OleDbCommandBuilder(myDataAdapter); 
    myAccessConn.Open(); 

    // Get Analogue table 
    int rowCount = myDataAdapter.Fill(myDataSet, "Analogue"); 
    Console.WriteLine("Row Count: " + rowCount); 
    DataTable analogues = myDataSet.Tables["Analogue"]; 
    analogues.PrimaryKey = new DataColumn[] { analogues.Columns["Name"] }; 

    // Create a new row. 
    System.Data.DataRow newRow; 
    newRow = analogues.NewRow(); 
    newRow["Name"] = "My new row"; 
    newRow["Area"] = "System"; 
    newRow["Description"] = "My new row, created by me!"; 

    analogues.Rows.Add(newRow); 
    myDataAdapter.InsertCommand = myCommandBuilder.GetInsertCommand(); 
    myDataAdapter.UpdateCommand = myCommandBuilder.GetUpdateCommand(); 
    Console.WriteLine("Insert Command: " + myDataAdapter.InsertCommand.ToString()); 
    Console.WriteLine("Update Command: " + myDataAdapter.UpdateCommand.ToString()); 

    myDataAdapter.Update(myDataSet, "Analogue"); 
} 
catch (Exception ex) 
{ 
    Console.WriteLine("Error: Failed to manipulate the DataBase.\n{0}", ex.ToString()); 
    return; 
} 
finally 
{ 
    myAccessConn.Close(); 
} 

我已刪除大多數列從表簡潔,但所有的列標題都在報價,他們沒有包含任何空格,且都不保留字。我沒有刪除的是Access關鍵字,但我注意到「NAME」和「Description」是。我不相信這會導致我的問題,因爲我已經能夠修改現有行中「說明」列中的文本。

命令生成器生成以下對我來說這似乎不太對勁:

System.Data.OleDb.OleDbCommand 

我需要手動建立這個命令?如果是這樣,我應該使用插入或更新 - 適配器只有一個更新方法,據我所知可以調用更新或插入?

這裏是堆棧跟蹤與錯誤:

System.Data.OleDb.OleDbException (0x80040E14): Syntax error in INSERT INTO statement. 
    at System.Data.Common.DbDataAdapter.UpdatedRowStatusErrors(RowUpdatedEventArgs rowUpdatedEvent, BatchCommandInfo[] batchCommands, Int32 commandCount) 
    at System.Data.Common.DbDataAdapter.UpdatedRowStatus(RowUpdatedEventArgs rowUpdatedEvent, BatchCommandInfo[] batchCommands, Int32 commandCount) 
    at System.Data.Common.DbDataAdapter.Update(DataRow[] dataRows, DataTableMapping tableMapping) 
    at System.Data.Common.DbDataAdapter.UpdateFromDataTable(DataTable dataTable, DataTableMapping tableMapping) 
    at System.Data.Common.DbDataAdapter.Update(DataSet dataSet, String srcTable) 
    at SystemMonitor.MainWindow.mnuGenerateSMACS6Interface_Click(Object sender, EventArgs e) in c:\iogen\code\MainWindow.cs:line 436 
+2

可幫助:http://stackoverflow.com/q/19557920/284240 –

+0

最好的Q是我s他的SQL插入語句的形式構建數據集...有錯誤 – apomene

+3

'Console.WriteLine(myDataAdapter.InsertCommand.CommandText);'現在你打印的只是類的名稱而不是屬性commandtext – Steve

回答

2

,如果你在SelectCommand.CommandText你有SELECT * FROM Analogue,然後由OleDbCommandBuilder建成InsertCommand包含每個列的INSERT列。
如果你想從INSERT中刪除一些列,那麼你需要自己構建命令。

但是,語法錯誤的起源是Access 2007中保留字的存在,如NAME和DESCRIPTION。解決方案很簡單,Mr Schmelter已經指出其評論。

OleDbCommandBuilder myCommandBuilder = new OleDbCommandBuilder(myDataAdapter); 
myCommandBuilder.QuotePrefix = "["; 
myCommandBuilder.QuoteSuffix = "]"; 

這將迫使OleDbCommandBuilder構建CommandText中使用適當的前綴和後綴周圍的列名和表名導致的CommandText INSERT和UPDATE像

"INSERT INTO [Analogue] ([Name], [Area], [Description], ...) VALUES(?, ?, ?, ...)" 

的DataAdapter.UpdateCommand執行INSERT/UPDATE/DELETE命令查看已更改的DataTable中該行的DataRow.RowState屬性

相關問題