语法错误在日期和时间查询表达式中缺少运算符

时间:2017-10-24 09:34:12

标签: c# winforms

cmd.CommandText = "SELECT * FROM " + TableName + " WHERE"
                                                + " [TIME_STAMP]=#" + TimeStamp.ToString("dd-MM-yyyy HH:mm") + "#"
                                                + " AND [Slave_Id]='" + SlaveUnitID
                                                + " AND [Parameter]='" + Param
                                                + "' AND [Status]='" + Status
                                                + "' AND [Reading]='" + Reading + "'";
                                dr = cmd.ExecuteReader();

我在上面的查询中收到错误。但是得到同样的结果,Plz帮助它

1 个答案:

答案 0 :(得分:0)

尝试使用参数化查询

conn.Open();
string query = "SELECT * FROM " + TableName + " WHERE [TIME_STAMP] =@date "
+" AND [Slave_Id]=@sid AND [Parameter] =@param  "
+" AND [Status] =@status AND [Reading] = @reading"
qlCommand cmd = new SqlCommand(query,conn);
cmd.Parameters.AddWithValue("@date", TimeStamp);
cmd.Parameters.AddWithValue("@sid", SlaveUnitID);
cmd.Parameters.AddWithValue("@param", Param);
cmd.Parameters.AddWithValue("@status", Status);
cmd.Parameters.AddWithValue("@reading", Reading );
dr = cmd.ExecuteReader();