【问题标题】:update query in access db having text datatype在具有文本数据类型的访问数据库中更新查询
【发布时间】:2013-05-04 05:21:00
【问题描述】:
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 具有文本数据类型。 上述查询未更新表。

【问题讨论】:

  • 尝试将您的@paramUserPassword 用单引号括起来

标签: c# ms-access-2007 oledb


【解决方案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,它将确保在退出块时处置OleDbConnectionOleDbCommand 对象使用的资源。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-23
    • 1970-01-01
    • 1970-01-01
    • 2010-09-23
    相关资源
    最近更新 更多