![](/img/trans.png)
[英]System.Data.OleDb.OleDbException (0x80040E14): Syntax error (missing operator) in query expression 'Email= '12' and Password '12''
[英]Error: System.Data.OleDb.OleDbException (0x80040E14): Syntax error (missing operator) in query expression
我試圖將存儲過程的結果導出到 Excel 並在運行時收到以下錯誤: System.Data.OleDb.OleDbException (0x80040E14): 查詢表達式中的語法錯誤(缺少運算符)。 下面是我正在使用的代碼。 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;
所以這個問題與你的數據中有一個未轉義的單引號有關。 處理該問題的兩個選項是轉義單引號(將其轉換為兩個單引號):
string myData = "This string won't work";
string myDataEscaped = myData.Replace("'", "''");
或者另一個選項(更健壯)是使用參數化查詢。 我將僅使用代碼的下半部分,您將在其中執行此查詢構建和插入,並向您展示如何完成(以及一些清理和使用StringBuilder
)。 注意我沒有編譯和測試這個,因為我沒有任何測試數據可以使用。
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.