2012-01-20 87 views
0

這裏是例外:INSERT INTO語句中的語法錯誤。誰能幫我這個?

  System.Data.OleDb.OleDbException was unhandled 
       Message=Syntax error in INSERT INTO statement. 
       Source=Microsoft Office Access Database Engine 
       ErrorCode=-2147217900 
       StackTrace: 
        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 SyndicateII.Form1.NavRec() in C:\Documents and Settings\David\Desktop\SyndicateII\SyndicateII\Form1.cs:line 787 
        at SyndicateII.Form1.button48_Click(Object sender, EventArgs e) in C:\Documents and Settings\David\Desktop\SyndicateII\SyndicateII\Form1.cs:line 1298 
        at System.Windows.Forms.Control.OnClick(EventArgs e) 
        at System.Windows.Forms.Button.OnClick(EventArgs e) 
        at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) 
        at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) 
        at System.Windows.Forms.Control.WndProc(Message& m) 
        at System.Windows.Forms.ButtonBase.WndProc(Message& m) 
        at System.Windows.Forms.Button.WndProc(Message& m) 
        at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) 
        at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) 
        at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) 
        at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) 
        at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData) 
        at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) 
        at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) 
        at System.Windows.Forms.Application.Run(Form mainForm) 
        at SyndicateII.Program.Main() in C:\Documents and Settings\David\Desktop\SyndicateII\SyndicateII\Program.cs:line 18 
        at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) 
        at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) 
        at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 
        at System.Threading.ThreadHelper.ThreadStart_Context(Object state) 
        at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx) 
        at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 
        at System.Threading.ThreadHelper.ThreadStart() 
       InnerException: 

這裏是我使用的一個新行添加到我的Access 2007數據庫的代碼塊。 這段代碼在我的db.open和db.close代碼之外。 我以前得到一個連接錯誤,但我註釋掉了db.dispose行,現在它給了我一個語法錯誤。 我試着做一個rDS.InsertCommand。這並沒有返回任何錯誤,但它沒有將新行插入到數據庫中。 我從來沒有在我的代碼之前這樣做過,所以我不會possitvie如果我正在這個正確的方式。 我想知道如果問題是當我的Form1_Load數據庫連接。 我使用了Microsoft.ACE.OLEDB.12.0,但在尋找幫助時發現我的錯誤,似乎大多數人都在使用Microsoft.JET.OLEDB.4.0。 我試着切換到,但這也給我一個錯誤。 我真的不知道問題是什麼,我希望我能得到一些幫助。過去幾天我一直盯着我的電腦試圖自己弄清楚。但我沒有運氣。

  string path = "c:\\" + textBox269.Text + "_" + id + ".wav"; 

      //Add recording to the listview 
      item = new ListViewItem(textBox269.Text); 
      item.SubItems.Add(richTextBox11.Text); 
      item.SubItems.Add(DateTime.Now.ToString()); 
      item.SubItems.Add(path); 
      listView1.Items.Add(item); 

      System.Data.OleDb.OleDbCommandBuilder cb; 
      cb = new System.Data.OleDb.OleDbCommandBuilder(rAD); 

      DataRow rRow2 = rDS.Tables["Recordings"].NewRow(); 

      rRow2[1] = id; 
      rRow2[2] = textBox269.Text; 
      rRow2[3] = richTextBox11.Text; 
      rRow2[4] = DateTime.Now.ToString(); 
      rRow2[5] = path; 

      //rRow2["RefID"] = id; 
      //rRow2["RecName"] = textBox269.Text; 
      //rRow2["Desc"] = richTextBox11.Text; 
      //rRow2["DateAdded"] = DateTime.Now.ToString(); 
      //rRow2["FileLocation"] = path; 

      rDS.Tables["Recordings"].Rows.Add(rRow2); 

      rMaxRows = rMaxRows + 1; 
      rInc = rInc + 1; 

      rAD.Update(rDS, "Recordings"); <--this line is where the error occurs 
+0

什麼是模式? 「錄音」中有多少列?另外,rRow應該使用基於0的索引,1-5應該可以是0-4? –

+0

有6列,所以我使用0-5指數。但我不想在0索引中放入任何東西,因此在數據庫中會有一個自動編號列。但是,我還應該包括0指數嗎?我不必將它作爲自動編號。實際上我並不需要那個專欄。它的Id列自動作爲主鍵。 – user1161086

+0

我會檢查所有類型是否正確,並且 - 如果DateAdded列不是字符串 - 請移除ToString()。沒有看到架構任何進一步的分析是一種困難:) –

回答

0

插入一行到MS-訪問,試着將下面的代碼來使用你的需求:

public void InsertRow() 
{ 
    try 
    { 
    using (OleDbConnection con = new OleDbConnection()) 
    { 
     con.ConnectionString = Users.GetConnectionString(); // your ms-access connection string 
     con.Open(); 
     OleDbCommand cmd = new OleDbCommand(); 
     cmd.Connection = con; 
     cmd.CommandType = CommandType.Text; 
     cmd.CommandText = "INSERT INTO Recordings(RecName,Desc,DateAdded, FileLocation) VALUES(id,textBox269.Text,richTextBox11.Text,DateTime.Now,path)"; 
     cmd.ExecuteNonQuery(); 
    } 
    } 
    catch (Exception ex) 
    { 
    throw new Exception(ex.Message); 
    } 
} 
+0

我仍然收到相同的錯誤。 – user1161086

+0

你的連接字符串是什麼樣的?你的表格定義也是有幫助的。 –

+0

con.ConnectionString =「Provider = Microsoft.ACE.OLEDB.12.0; Data Source = U:/ Syndicate II/Syndicate II.accdb; Persist Security Info = False」; – user1161086

1

更換

rAD.Update(rDS, "Recordings"); 

與此:

rAD.Update(rDS.Tables["Recordings"]);