C#OleDBCommand SQL语法错误

时间:2012-06-26 06:23:08

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

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

我尝试过改变数据,没有成功。这个源是一个XML文件。正确加载XML数据。

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

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: 

2 个答案:

答案 0 :(得分:5)

好的,我有一个答案(感谢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)";

答案 1 :(得分:1)

使用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();
}