【问题标题】:C# OleDBCommand SQL Syntax ErrorC# OleDBCommand SQL 语法错误
【发布时间】:2014-07-10 23:27:47
【问题描述】:

我在通过 C# OleDbCommand 对 Access 后端表运行插入查询时遇到问题。如果您复制它并直接在 Access 中运行,SQL 运行良好,并且在点击此特定条目之前它适用于其他数据。

我已尝试更改数据,但没有成功。其来源是一个 XML 文件。 XML 数据正在正确加载。

失败的命令是带有错误“INSERT INTO 语句中的语法错误”的 dbCommand.ExecuteNonQuery()。

dbCommand = new OleDbCommand(string.Format(INSERT_RECURRING, tableName, fieldList, valueList), dbCon);
dbCommand.ExecuteNonQuery();

下面是尝试运行但失败的 SQL。请注意,表名和字段名是从 XML 源文件生成的,因此需要大写和下划线。

INSERT INTO LIST_CONTACTSGP (RID, CONTACT_TYPE, SURNAME_OR_FAMILY_NAME, GIVEN_NAMES, ORGANISATION, ADDRESS_GP, PHONE, MOBILE, WORK, FAX, EMAIL, COMMENTS) VALUES ('1-8HZAZN', 'Daughter, Other Emergency Contact', 'Smith', 'Ms Jenny', '', '', '49123456', '0416123456', '', '', '', '');

正如我之前提到的,此 SQL 可以在 Access 中正常运行,并且其他插入命令在此失败之前可以正常工作(在其他表上)。我删除了所有可能导致问题的 SQL 特殊字符。

任何帮助将不胜感激。

根据要求,堆栈跟踪的副本:

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.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr)
       at System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult)
       at System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult)
       at System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult)
       at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method)
       at System.Data.OleDb.OleDbCommand.ExecuteNonQuery()
       at CCAPXMLImport.CXIDatabase.insertRepeatedSection(Dictionary`2 ReferralData, String ReferralID) in C:\SMNS-local\Development\TeleHealth\CCAPXMLImport\CCAPXMLImport\CCAPXMLImport\CXIDatabase.cs:line 279
       at CCAPXMLImport.frmMain.btnBrowse_Click(Object sender, EventArgs e) in C:\SMNS-local\Development\TeleHealth\CCAPXMLImport\CCAPXMLImport\CCAPXMLImport\frmMain.cs:line 131
       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 CCAPXMLImport.Program.Main() in C:\SMNS-local\Development\TeleHealth\CCAPXMLImport\CCAPXMLImport\CCAPXMLImport\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: 

【问题讨论】:

  • 表格中的所有字段都是文本(255),但备注字段是备忘录格式。我已尝试将其更改为 Text(255),因为我们过去曾遇到过备忘录字段的问题。
  • 我会用 [] 封装列名,尤其是列 cmets 对我来说似乎很可疑。
  • 您确定您的表允许零长度字符串吗?默认情况下,Access 表不允许 ZLS,因此那些空字符串将是一个问题。 (@ralf.w。我没有看到任何保留字support.microsoft.com/kb/286335
  • 嗨 Remou,我们的表确实允许 ZLS - 在此之前运行其他查询,主要使用 ZLS 作为值(可能偶尔会有数据 - 如果没有,我将添加代码以不运行插入稍后插入)。
  • Ralf.w,我将尝试封装列名,尽管之前运行的查询(没有失败)使用了 Comments 列。

标签: c# sql ms-access-2007 oledbcommand


【解决方案1】:

好的,我有答案了(感谢 ralf.w)。非常感谢 AVD 和 Remou 花时间帮助解决这个问题。

需要将列名封装在方括号中。尽管使用其他表并直接在 Access 中工作正常,但在通过 OleDbCommand 发送时,一个(或多个)列名肯定会导致问题。

所以 SQL 代码应该是:

string sql="INSERT INTO LIST_CONTACTSGP ([RID], [CONTACT_TYPE], [SURNAME_OR_FAMILY_NAME], 
            [GIVEN_NAMES], [ORGANISATION], [ADDRESS_GP], [PHONE], [MOBILE], [WORK], [FAX], [EMAIL], 
            [COMMENTS]) VALUES (@RID, @CONTACT_TYPE, @SURNAME_OR_FAMILY_NAME, 
            @GIVEN_NAMES, @ORGANISATION, @ADDRESS_GP, @PHONE, @MOBILE, @WORK, @FAX, 
             @EMAIL, @COMMENTS)";

【讨论】:

  • 感谢分享您的解决方案,很高兴能为您提供帮助。我过去在 OleDb/Access-SQL 解析方面遇到过类似的问题。
  • 我遇到了同样的问题。除了保存到 Access Boolean 字段中的字段外,所有字段都运行良好。去搞清楚。为什么只有这些字段,为什么它们需要括号,而其他字段不需要?无论如何,括号 [] 就像一个魅力。谢谢!!
【解决方案2】:

使用parameters 代替字符串连接。

string sql="INSERT INTO LIST_CONTACTSGP (RID, CONTACT_TYPE, SURNAME_OR_FAMILY_NAME, 
            GIVEN_NAMES, ORGANISATION, ADDRESS_GP, PHONE, MOBILE, WORK, FAX, EMAIL, 
            COMMENTS) VALUES (@RID, @CONTACT_TYPE, @SURNAME_OR_FAMILY_NAME, 
            @GIVEN_NAMES, @ORGANISATION, @ADDRESS_GP, @PHONE, @MOBILE, @WORK, @FAX, 
             @EMAIL, @COMMENTS)";

using(OleDbCommand dbCommand = new OleDbCommand(sql,conn))
{
  dbCommand.Parameters.Add("@RID",OleDbType.VarChar,20).Value="10";
  dbCommand.Parameters.Add("@CONTACT_TYPE",OleDbType.VarChar,20).Value="Somethig";
  .....
  conn.Open();
  dbCommand.ExecuteNonQuery();
  conn.Close();
}

【讨论】:

  • 感谢 AVD,我会试一试并尽快报告 :)
  • 抱歉 AVD,插入时仍然出现语法错误。我将把参数留在那里,因为我认为这是一个更好的方法。
  • @user1481804 参数方式最干净,最安全。您必须尝试并发布您的代码,异常跟踪。
  • WILCO - 只需要注释掉我的错误处理。我会在早上做第一件事,然后回来报告。谢谢:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-19
  • 1970-01-01
  • 2013-04-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多