当我运行以下查询时,我在查询表达式中出现语法错误。
diagrams
错误是:
查询表达式中的语法错误(缺少运算符)'(大小写时的情况) EmpName =' Sam'那么' 5.6'当EmpName =' shane'那么' 1.6'什么时候 EmpName =' Mike'那么' 0.8'端)'
答案 0 :(得分:1)
您的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 (?, ?, ?)"
;
<强>买者强>
未经测试的代码,源自文档。
答案 1 :(得分:0)
(EmpName =&#39; Sam&#39;然后&#39; 5.6&#39; .....)
您不必为数值使用引号。是&#34; PNTS&#34;在数据库中有一个字符串?如果没有尝试删除这些值的报价。