[英]syntax error missing operator in query expression
當我運行以下查詢時,查詢表達式中出現語法錯誤。
private void button8_Click(object sender, EventArgs e)
{
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
string query1 = "UPDATE Points SET PNTS =
(case when EmpName = '" + comboBox1.Text +
"' then '" + label15.Text + "' when EmpName = '" +
comboBox2.Text + "' then '" + label16.Text +
"' when EmpName = '" + comboBox3.Text + "' then '" +
label17.Text + "' end) WHERE EmpName in ('" +
comboBox1.Text + "', '" + comboBox2.Text + "', '" +
comboBox3.Text + "')";
command.CommandText = query1;
command.ExecuteNonQuery();
connection.Close();
}
錯誤是:
查詢表達式“((當EmpName ='Sam'然后是5.6',當EmpName ='shane'然后是'1.6',當EmpName ='Mike'然后'0.8'結束時的情況))語法錯誤(缺少運算符)。
您的sql中有語法錯誤(ms Access不包含大小寫表達式)。 如下重寫源代碼行:
string query1 =
"UPDATE Points SET PNTS = "
+ "SWITCH ("
+ " EmpName = '" + comboBox1.Text + "', '" + label15.Text + "'"
+ ", EmpName = '" + comboBox2.Text + "', '" + label16.Text + "'"
+ ", EmpName = '" + comboBox3.Text + "', '" + label17.Text + "'"
+ ", true, ''"
+ ")"
+ " WHERE EmpName in ('" + comboBox1.Text + "', '" + comboBox2.Text + "', '" + comboBox3.Text + "')"
;
為了應對sql注入的風險,請考慮使用參數化的sql,因為一些注釋者建議:
OleDbParameter parameter;
// The n-th generic placeholder in the sql string will be set to the n-th registered Parameter Value.
// '12' represents the data size, adjustment may be needed ( can possibly be dropped altogether )
parameter = command.Parameters.Add("@InputParm", OleDbType.VarChar, 12);
parameter.Value = comboBox1.Text;
parameter = command.Parameters.Add("@InputParm", OleDbType.VarChar, 12);
parameter.Value = label15.Text;
parameter = command.Parameters.Add("@InputParm", OleDbType.VarChar, 12);
parameter.Value = comboBox2.Text;
parameter = command.Parameters.Add("@InputParm", OleDbType.VarChar, 12);
parameter.Value = label16.Text;
parameter = command.Parameters.Add("@InputParm", OleDbType.VarChar, 12);
parameter.Value = comboBox3.Text;
parameter = command.Parameters.Add("@InputParm", OleDbType.VarChar, 12);
parameter.Value = label17.Text;
parameter = command.Parameters.Add("@InputParm", OleDbType.VarChar, 12);
parameter.Value = comboBox1.Text;
parameter = command.Parameters.Add("@InputParm", OleDbType.VarChar, 12);
parameter.Value = comboBox2.Text;
parameter = command.Parameters.Add("@InputParm", OleDbType.VarChar, 12);
parameter.Value = comboBox3.Text;
string query1 =
"UPDATE Points SET PNTS = "
+ "SWITCH ("
+ " EmpName = ?, ?"
+ " EmpName = ?, ?"
+ " EmpName = ?, ?"
+ ", true, ''"
+ ")"
+ " WHERE EmpName in (?, ?, ?)"
;
警告
未經測試的代碼,源自文檔。
(如果EmpName ='Sam'然后是'5.6'.....)
您不必對數值使用引號。 數據庫中的“ PNTS”是字符串嗎? 如果沒有,請嘗試刪除這些值的引號。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.