简体   繁体   English

错误:System.Data.OleDb.OleDbException (0x80040E14):查询表达式中的语法错误(缺少运算符)

[英]Error: System.Data.OleDb.OleDbException (0x80040E14): Syntax error (missing operator) in query expression

I'm trying to export the results of a stored procedure to Excel and receiving the following error when running: System.Data.OleDb.OleDbException (0x80040E14): Syntax error (missing operator) in query expression.我试图将存储过程的结果导出到 Excel 并在运行时收到以下错误: System.Data.OleDb.OleDbException (0x80040E14): 查询表达式中的语法错误(缺少运算符)。 Below is the code I am using.下面是我正在使用的代码。 The excel file gets created but there is no data populating the file. excel 文件已创建,但没有数据填充该文件。

string TableColumns = "";

// Get the Column List from Data Table so can create Excel Sheet with Header
foreach (DataTable table in ds.Tables)
{
    foreach (DataColumn column in table.Columns)
    {
        TableColumns += column + "],[";
    }
}

// Replace most right comma from Columnlist
TableColumns = ("[" + TableColumns.Replace(",", " Text,").TrimEnd(','));
TableColumns = TableColumns.Remove(TableColumns.Length - 2);

//Use OLE DB Connection and Create Excel Sheet
Excel_OLE_Con.ConnectionString = connstring;
Excel_OLE_Con.Open();
Excel_OLE_Cmd.Connection = Excel_OLE_Con;
Excel_OLE_Cmd.CommandText = "Create table " + SheetName + " (" + TableColumns + ")";
Excel_OLE_Cmd.ExecuteNonQuery();

//Write Data to Excel Sheet from DataTable dynamically
foreach (DataTable table in ds.Tables)
{
    String sqlCommandInsert = "";
    String sqlCommandValue = "";
    foreach (DataColumn dataColumn in table.Columns)
    {
        sqlCommandValue += dataColumn + "],[";
    }

    sqlCommandValue = "[" + sqlCommandValue.TrimEnd(',');
    sqlCommandValue = sqlCommandValue.Remove(sqlCommandValue.Length - 2);
    sqlCommandInsert = "INSERT into " + SheetName + "(" + sqlCommandValue + ") VALUES(";

    int columnCount = table.Columns.Count;
    foreach (DataRow row in table.Rows)
    {
        string columnvalues = "";
        for (int i = 0; i < columnCount; i++)
        {
            int index = table.Rows.IndexOf(row);
            columnvalues += "'" + table.Rows[index].ItemArray[i] + "',";
        }
        columnvalues = columnvalues.TrimEnd(',');
        var command = sqlCommandInsert + columnvalues + ")";
        Excel_OLE_Cmd.CommandText = command;
        Excel_OLE_Cmd.ExecuteNonQuery();
    }
}
Excel_OLE_Con.Close();
Dts.TaskResult = (int)ScriptResults.Success;

So the issue is related to you have an unescaped single quote in your data.所以这个问题与你的数据中有一个未转义的单引号有关。 Two options for dealing with that are to escape the single quote (turn it into two single quotes):处理该问题的两个选项是转义单引号(将其转换为两个单引号):

string myData = "This string won't work";
string myDataEscaped = myData.Replace("'", "''");

or the other option (and more robust) is to use a parameterized query.或者另一个选项(更健壮)是使用参数化查询。 I will use just the lower part of your code where you are doing this query build up and insertion and show you how that can be done (along with some cleanup and making use of StringBuilder ).我将仅使用代码的下半部分,您将在其中执行此查询构建和插入,并向您展示如何完成(以及一些清理和使用StringBuilder )。 Note I did not compile and test this as I don't have any test data to use.注意我没有编译和测试这个,因为我没有任何测试数据可以使用。

var tableColumns = string.Join(",", columns.Cast<DataColumn>().Select(x => "[" + x.ColumnName + "]"));
var insertBase = $"INSERT into {SheetName} ({tableColumns}) VALUES(";
int columnCount = table.Columns.Count;
foreach (DataRow row in table.Rows)
{
    Excel_OLE_Cmd.Parameters.Clear(); // Since you are reusing the command you have to clear the parameters

    var command = new StringBuilder(insertBase);
    var index = table.Rows.IndexOf(row);

    // I will assume you always have at least one column, otherwise these lines would fail
    // Add the first row before the loop that way we don't have to delete the end comma
    var param = $"@param_{index}_0";
    command.Append(param);
    Excel_OLE_Cmd.Parameters.AddWithValue(param, table.Rows[index].ItemArray[0]);
    for (int i = 1; i < columnCount; ++i)
    {
        param = $"@param_{index}_{i}"
        command.Append("," + param);
        Excel_OLE_Cmd.Parameters.AddWithValue(param, table.Rows[index].ItemArray[i]);
    }
    command.Append(")");
    Excel_OLE_Cmd.CommandText = command.ToString();
    Excel_OLE_Cmd.ExecuteNonQuery();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 System.Data.OleDb.OleDbException (0x80040E14):查询表达式中的语法错误(缺少运算符)电子邮件 = '12' 和密码 '12'' - System.Data.OleDb.OleDbException (0x80040E14): Syntax error (missing operator) in query expression 'Email= '12' and Password '12'' c#access System.Data.OleDb.OleDbException(0x80040E14):UPDATE语句中的语法错误 - c# access System.Data.OleDb.OleDbException (0x80040E14): Syntax error in UPDATE statement C# 中的 SQL 查询(System.Data.OleDb.OleDbException:&#39;查询表达式中的语法错误(缺少运算符)) - SQL query in C# (System.Data.OleDb.OleDbException: 'Syntax error (missing operator) in query expression) 消息:System.Data.OleDb.OleDbException:查询表达式中的语法错误(缺少运算符) - Message: System.Data.OleDb.OleDbException : Syntax error (missing operator) in query expression C#Oledb 0x80040E14插入错误 - C# Oledb 0x80040E14 INSERT INTO error C#中的insert into语句(0x80040E14)中的oledb异常语法错误 - oledb exception syntax error in insert into statement (0x80040E14) in C# SELECT语句中的OleDbCommand语法错误0x80040E14 - OleDbCommand syntax error 0x80040E14 in SELECT Statement OleDbException (x80040E14):Insert into 语句中的语法错误 - OleDbException (x80040E14): Syntax error in Insert into statement System.Data.OleDb.OleDbException:INSERT INTO语句中的语法错误 - System.Data.OleDb.OleDbException: Syntax error in INSERT INTO statement System.Data.OleDb.OleDbException: 'UPDATE 语句中的语法错误。 ' - System.Data.OleDb.OleDbException: 'Syntax error in UPDATE statement. '
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM