INSERT INTO语句中的语法错误。谁能帮我这个?

时间:2012-01-20 17:24:00

标签: c# syntax-error oledbdataadapter oledbexception newrow

以下是例外:

            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

2 个答案:

答案 0 :(得分:1)

替换

rAD.Update(rDS, "Recordings");

用这个:

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

答案 1 :(得分: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);
  }
}