简体   繁体   English

为什么 MySQL 在 C# 代码中返回总是 1 而在我在存储过程中测试时不是?

[英]Why MySQL return is always 1 in C# code but not when I test in stored procedure?

I am developing an ASP.net application using MySQL and have a question related to a stored procedure return value.我正在使用 MySQL 开发 ASP.net 应用程序,并且有一个与存储过程返回值相关的问题。

This is my stored procedure:这是我的存储过程:

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

When I test this code with following MySQL script I get 0 ( @ProfileUpdated ) always when there is no update.当我使用以下 MySQL 脚本测试此代码时,我总是在没有更新时得到 0 ( @ProfileUpdated )。

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

But when I check this in my C# code, it is always showing 1 ( 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();
}

Why is the difference between MySQL script vs C# code?为什么 MySQL 脚本与 C# 代码之间存在差异?

Unless you have set the UseAffectedRows connection string option, it defaults to false .除非您设置了UseAffectedRows连接字符串选项,否则它默认为false This means :这意味着

When false (default), the connection reports found rows instead of changed (affected) rows.当为false (默认)时,连接报告找到行而不是已更改(受影响)的行。 Set to true to report only the number of rows actually changed by UPDATE or INSERT … ON DUPLICATE KEY UPDATE statements.设置为true以仅报告UPDATEINSERT … ON DUPLICATE KEY UPDATE语句实际更改的行数。

Additionally, from the documentation of the ROW_COUNT function :此外,从ROW_COUNT函数文档中

For UPDATE statements, the affected-rows value by default is the number of rows actually changed.对于UPDATE语句,默认情况下受影响的行值是实际更改的行数。 If you specify the CLIENT_FOUND_ROWS flag to mysql_real_connect() when connecting to mysqld [ed.如果在连接到 mysqld 时将CLIENT_FOUND_ROWS标志指定给mysql_real_connect() [ed. note: this is the same as UseAffectedRows ], the affected-rows value is the number of rows “found”;注意:这与UseAffectedRows ] 相同,受影响的行值是“找到”的行数; that is, matched by the WHERE clause.也就是说,由WHERE子句匹配。

Thus, the UPDATE user statement in your stored procedure will return the number of rows that were found by the query, not the number that were actually updated.因此,存储过程中的UPDATE user语句将返回查询找到的行数,而不是实际更新的行数。

To fix this, either:要解决此问题,请执行以下任一操作:

  1. Set UseAffectedRows=true;设置UseAffectedRows=true; in your connection string;在您的连接字符串中; this may cause changes to other UPDATE queries.这可能会导致其他UPDATE查询发生更改。
  2. Add more conditions to the WHERE clause, eg, WHERE ... AND SELLER_IMAGE_ID != SellerImageID AND SELLER_BUSINESS_LOGO_ID != BusinessImageID , to make sure the row is only found and updated if it actually needs to change.WHERE子句添加更多条件,例如, WHERE ... AND SELLER_IMAGE_ID != SellerImageID AND SELLER_BUSINESS_LOGO_ID != BusinessImageID ,以确保仅在实际需要更改行时才找到并更新该行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM