strSQL = @"UPDATE UserLogin
SET UserPassword= @paramUserPassword
WHERE UserId= @paramUserId";
objOleDbCommand = new OleDbCommand(strSQL, connectionstring);
objOleDbCommand.Parameters.AddWithValue("@paramUserId", "1");
objOleDbCommand.Parameters.AddWithValue("@paramUserPassword", "ooo");
objOleDbComm.ExecuteNonQuery(objOleDbCommand);
UserPassword和UserId具有text数据类型。 表格不会与上述查询一起更新。
答案 0 :(得分:1)
您未正确设置OleDbCommand
查询参数。正如OleDbCommand.Parameters Property的MSDN文章中所述,OleDbCommand对象不支持您使用它们的方式的命名参数。您将使用问号字符作为参数的占位符,然后以与查询中显示的顺序完全相同的顺序声明参数。
试试这个:
var strSQL = @"UPDATE UserLogin
SET UserPassword= ?
WHERE UserId= ?";
using (var myConnection = new OleDbConnection(connectionstring))
using (var objOleDbCommand = new OleDbCommand(strSQL, myConnection)) {
// Parameter used for the SET statement declared before the parameter for the WHERE
// clause since this parameter is used before that one in the SQL statement.
objOleDbCommand.Parameters.AddWithValue("@paramUserPassword", "ooo");
objOleDbCommand.Parameters.AddWithValue("@paramUserId", "1");
myConnection.Open();
objOleDbCommand.ExecuteNonQuery();
}
此代码还演示了using
statement,它将确保在退出块时处理OleDbConnection
和OleDbCommand
对象使用的资源。