【问题标题】:Syntax error in INSERT INTO statement. Can anyone help me with this?INSERT INTO 语句中的语法错误。谁能帮我这个?
【发布时间】:2012-02-15 06:02:03
【问题描述】:

这是一个例外:

            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

【问题讨论】:

  • 什么是架构? “录音”中有多少列?另外,rRow 应该使用基于 0 的索引,1-5 应该是 0-4?
  • 有 6 列,所以我使用的是 0-5 索引。但我不想在 0 索引中放入任何内容,因为它是数据库中的自动编号列。但是我还应该包括 0 索引吗?我不必将其作为自动编号。老实说,我实际上并不需要那个专栏。它是自动作为主键的 Id 列。
  • 我会检查所有类型是否正确并且 - 如果 DateAdded 列不是字符串 - 删除 ToString()。如果没有看到架构,任何进一步的分析都很难:)

标签: c# syntax-error oledbdataadapter oledbexception newrow


【解决方案1】:

替换

rAD.Update(rDS, "Recordings");

用这个:

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

【讨论】:

    【解决方案2】:

    要在 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);
      }
    }
    

    【讨论】:

    • 你的连接字符串是什么样的?您的表定义也会有所帮助。
    • con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source = U:/Syndicate II/Syndicate II.accdb;Persist Security Info = False";
    • 抱歉,我不确定您所说的表定义是什么意思。
    • 表定义是指Access Table Recordings和你设置的数据类型和其他属性。
    • 我的录音表有 6 列。 ID 是一种自动编号数据类型。 RefID 是数字数据类型。 RecName 和 Desc 是文本数据类型。 DateAdded 是日期/时间数据类型。而 FileLocation 是一种超链接数据类型。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多