0

这是一个例外:

            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。这没有返回任何错误,但它没有将新行插入数据库。我以前从来没有在我的代码中这样做过,所以如果我以正确的方式去做,我不会相信。我想知道问题是否出在我的 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
4

2 回答 2

1

代替

rAD.Update(rDS, "Recordings");

有了这个:

rAD.Update(rDS.Tables["Recordings"]); 
于 2012-06-25T19:05:25.260 回答
0

要在 ms-access 中插入一行,请尝试修改以下代码以满足您的需求:

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);
  }
}
于 2012-01-20T17:50:41.600 回答