c# - 为什么MySQL在C#代码中返回总是1,但在存储过程中测试时却不是?

标签 c# mysql .net

我正在使用 MySQL 开发 ASP.net 应用程序,并且有一个与存储过程返回值相关的问题。

这是我的存储过程:

CREATE DEFINER=`pcg`@`%` PROCEDURE `UpdatePreSellerProfile`(
IN UserID INT(11),
IN SellerImageID INT(11),
IN BusinessImageID INT(11),
OUT ProfileUpdated INT(1)
)
BEGIN
SET @Approved = 'APPROVED';
UPDATE user SET 
    SELLER_IMAGE_ID = COALESCE((SELECT IMAGE_ID FROM image_url WHERE IMAGE_USER_ID = UserID AND IMAGE_ID=SellerImageID),SELLER_IMAGE_ID),
    SELLER_BUSINESS_LOGO_ID = COALESCE((SELECT IMAGE_ID FROM image_url WHERE IMAGE_USER_ID = UserID AND IMAGE_ID=BusinessImageID),SELLER_BUSINESS_LOGO_ID)
WHERE (USER_LOGIN_ID = UserID AND USER_PROFILE_STATUS = @Approved);
SET ProfileUpdated = ROW_COUNT();
END

当我使用以下 MySQL 脚本测试此代码时,如果没有更新,我总是得到 0 (@ProfileUpdated)。

call UpdatePreSellerProfile(@UserID, @SellerImageID, @BusinessImageID ,@ProfileUpdated);

但是当我在 C# 代码中检查这一点时,它始终显示 1 (ProfileUpdated)。

if (oMySQLConnecion.State == System.Data.ConnectionState.Open)
{
    MySqlCommand oCommand = new MySqlCommand("UpdatePreSellerProfile", oMySQLConnecion);
    oCommand.CommandType = System.Data.CommandType.StoredProcedure;

    MySqlParameter sqlProfileUpdated = new MySqlParameter("@ProfileUpdated", MySqlDbType.VarString);
    sqlProfileUpdated.Direction = System.Data.ParameterDirection.Output;
    oCommand.Parameters.Add(sqlProfileUpdated);

    oCommand.Parameters.AddWithValue("@UserID", UserID);
    oCommand.Parameters.AddWithValue("@SellerImageID", oSeller.SellerImageID);
    oCommand.Parameters.AddWithValue("@BusinessImageID", oSeller.BusinessLogoID);

    oCommand.ExecuteNonQuery();
    Int16 ProfileUpdated = Convert.ToInt16(oCommand.Parameters["@ProfileUpdated"].Value);

    if (ProfileUpdated > 0) // <<-- Should be greater only if it is updated is sucessfull
    {
        oDBStatus.Type = DBOperation.SUCCESS;
        oDBStatus.Message.Add(DBMessageType.SUCCESSFULLY_DATA_UPDATED);
    }
    else
    {
        oDBStatus.Type = DBOperation.ERROR;
        oDBStatus.Message.Add(DBMessageType.ERROR_NO_RECORDS_UPDATED);
    }
    oMySQLConnecion.Close();
}

为什么 MySQL 脚本与 C# 代码之间存在差异?

最佳答案

除非您设置了 UseAffectedRows 连接字符串选项,否则它默认为 falseThis means :

When false (default), the connection reports found rows instead of changed (affected) rows. Set to true to report only the number of rows actually changed by UPDATE or INSERT … ON DUPLICATE KEY UPDATE statements.

此外,来自documentation of the ROW_COUNT function :

For UPDATE statements, the affected-rows value by default is the number of rows actually changed. If you specify the CLIENT_FOUND_ROWS flag to mysql_real_connect() when connecting to mysqld [ed. note: this is the same as UseAffectedRows], the affected-rows value is the number of rows “found”; that is, matched by the WHERE clause.

因此,存储过程中的 UPDATE user 语句将返回查询找到的行数,而不是实际更新的行数。

要解决此问题,可以:

  1. 在连接字符串中设置UseAffectedRows=true;;这可能会导致其他UPDATE查询发生变化。
  2. WHERE 子句添加更多条件,例如 WHERE ... AND SELLER_IMAGE_ID != SellerImageID AND SELLER_BUSINESS_LOGO_ID != BusinessImageID,以确保仅找到该行并在确实需要更改时进行更新。

关于c# - 为什么MySQL在C#代码中返回总是1,但在存储过程中测试时却不是?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59701480/

相关文章:

C# 特殊字符的正则表达式是什么

c# - 横向的 WPF 到 XPS

php - createQueryBuilder/加入列 - Symfony

.net - 在单个模板多元素站点中组织 CSS 和 Javascript

c# - dotnet-razor-tooling.dll 的 .NET Core 1.0 部署问题

c# - 全局访问单例的Silverlight应用资源

c# - FileNet P8 工作场所 token 问题

MySql 按日期过滤复杂查询

PHP登录系统要求用户登录两次

.net - 为什么 IIS7 不变文化默认为 en-US?