1

我有 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.at dbCommand.ExecuteNonQuery()。如何处理这个问题?

4

1 回答 1

2

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

UPDATE tblUser set username = ?, [password] = ? where ID = 1
于 2013-04-13T09:58:34.787 回答