如何解决 OleDb 错误“FROM 子句中的语法错误”

本文关键字:错误 子句 FROM 语法 解决 OleDb 何解决 | 更新日期: 2023-09-27 17:55:17

目前我正在尝试使用此函数导入CSV文件:

    public DataSet ImportCommaSeparatedValueFileToDataSet(string SourceFile)
    {
        var dsImportCSVtoDataSetReturn = new DataSet();
        using (var objAdapter1 = new OleDbDataAdapter())
        {
            String sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + SourceFile.Substring(0, SourceFile.LastIndexOf(@"'")) + ";Extended Properties='text;HDR=Yes;FMT=Delimited'";
            var objConnection = new OleDbConnection(sConnectionString);
            objConnection.Open();
            var objCmdSelect = new OleDbCommand("SELECT * FROM " + SourceFile, objConnection);
            objAdapter1.SelectCommand = objCmdSelect;
            objAdapter1.Fill(dsImportCSVtoDataSetReturn);
            objConnection.Close();
        }
        return dsImportCSVtoDataSetReturn;
    }

当我尝试导入文件名中没有空格的文件时,它工作正常。 当我尝试导入以下文件时:

D:'Workspace'WoldCard export.csv

然后我收到以下异常:

excException = {"Syntax error in FROM clause."}
Source = "Microsoft JET Database Engine"
StackTrace  "   at System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr)'r'n   at System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult)'r'n   at System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult)'r'n   at System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult)'r'n   at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method)'r'n   at System.Data.OleDb.OleDbCommand.ExecuteReader(CommandBehavior behavior)'r'n   at System.Data.OleDb.OleDbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior)'r'n   at System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)'r'n   at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)'r'n   at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet)'r'n   at CommonObjects4.clsUtilityOffice.ImportCommaSeparatedValueFileToDataSet(String SourceFile) in D:''DevProjects''CommonObjects4''classes''clsUtilityOffice.cs:line 262" string

因此,很明显,问题在于SQL子句中的文件名中有一个空格;但是,当我尝试使用单引号来解决问题时:

var objCmdSelect = new OleDbCommand("SELECT * FROM '" + SourceFile + "'", objConnection); 

然后我收到此异常:

excException = {"''D:'Workspace'WoldCard export.csv'' is not a valid name.  Make sure that it does not include invalid characters or punctuation and that it is not too long."}

另外,当我尝试使用参数时:

var objCmdSelect = new OleDbCommand("SELECT * FROM @SourceFile", objConnection);
objCmdSelect.Parameters.Add("@SourceFile", SqlDbType.NVarChar).Value = SourceFile;

然后我收到此异常:

excException = {"Syntax error in query.  Incomplete query clause."}

另外,我后来从 http://social.msdn.microsoft.com/Forums/en-US/sqldataaccess/thread/1c399bf7-a6b3-47bb-8897-d4247b4938f0 了解到表名不能是参数。 有人有什么建议吗? 蒂亚。

如何解决 OleDb 错误“FROM 子句中的语法错误”

感谢您的反馈,DJ KRAZE,它帮助我以不同的方式思考问题。 事实证明,如果名称中有空格,我只需要在表名周围添加方括号,尽管它必须只是文件名而不是完整路径:

var objCmdSelect = new OleDbCommand("SELECT * FROM [" + SourceFile.Substring(SourceFile.LastIndexOf(@"'") + 1, SourceFile.Length - SourceFile.LastIndexOf(@"'") - 1) + "]", objConnection);

有关更多详细信息,请参阅 sql 语句中的 [] 括号。

> Roger 尝试将连接字符串更改为如下所示

string fileName = SourceFile;   
string sConnectionString = string.Format(@"Provider=Microsoft.Jet.OLEDB.4.0; Data Source={0}; " + "Extended Properties='"text;HDR=YES;FMT=TabDelimited;'"", fileName);

文件路径和名称 D:''Workspace''WoldCard 导出.csv

您可以执行以下 2 件事之一,在文件名中添加下划线

D:'Workspace'WoldCard_export.csv 或在文件名两边添加双引号

@"D:'Workspace'" + "WoldCard export.csv"; 也看看Path.Combine Method

当您尝试添加参数时,请查看使用

Parameters.AddWithValues(@paramname, paramvalue)方法也是如此

你必须使用 [] 括号。带有空格、短划线 (-)、保留字等的文件名或表名...不行,将它们放在方括号中。

详细信息:请参阅:https://msdn.microsoft.com/en-us/library/ms175874.aspx