OleDbCommand在有效命令文本上抛出异常

时间:2013-04-13 09:57:35

标签: c# sql ms-access oledb

我有OleDbConnection连接到访问数据库。但是当我执行查询时,OleDbCommand会抛出异常,说该命令无效。我完全相信命令是有效的。

 string ret = "";

string command = "UPDATE tblUser set username = ?, password = ? where ID = 1";
dbConn.Open();

using (var dbCommand = dbConn.CreateCommand())
{
    dbCommand.Connection = dbConn;
    dbCommand.CommandText = command;
    dbCommand.Parameters.AddWithValue("?", username);
    dbCommand.Parameters.AddWithValue("?", password);

    int temp = dbCommand.ExecuteNonQuery();
    if (temp > 0)
        ret = "sukses";
    dbConn.Close();                
}
return ret;

但是,当执行上面的代码时,它会在Syntax error in UPDATE statement.处抛出异常dbCommand.ExecuteNonQuery()。如何处理这个问题?

1 个答案:

答案 0 :(得分:2)

你需要用括号转义PASSWORD,因为它是一个保留关键字,

UPDATE tblUser set username = ?, [password] = ? where ID = 1