为什么此存储过程总是返回1进行删除操作?

时间:2019-06-23 15:30:49

标签: c# mysql

我尝试了许多解决方案,但到目前为止都没有一个解决方案。这是我的第一个在存储过程中使用OUT参数的代码。

当我在mySQL WB中测试存储过程时,它按预期工作,但是当我在C#代码中读取该存储过程时,它总是返回1。但是,如果我为存储过程OUT参数硬编码零,它将在C#中读取该值。因此,存储过程很可能无法按预期工作,但无法找出原因。

存储过程:

CREATE DEFINER=`flashLight`@`%` PROCEDURE `DeleteAddress`(IN UserID INT(11),IN AddressID INT(11), OUT AddressDeleted INT(1))
BEGIN
SET @NotLocked = (0); 
SET @AddressIDExists = 1;
SET @Deleted = (1);
CALL IsAddressIDInUse(UserID, AddressID, @AddressIDExists); 
UPDATE address 
SET ADDRESS_DELETED = @Deleted
WHERE 
    ADDRESS_USER_ID = UserID AND 
    ADDRESS_ID = AddressID AND 
    LOCKED = @NotLocked AND 
    @AddressIDExists = 0;   
SET @DeletedRecords = ROW_COUNT();
SET AddressDeleted = IF(@DeletedRecords>0,1,0);
END

mySQL WB中的存储过程测试代码

SET @UserID = 160;
SET @AddressID = 229;
SET @AddressDeleted = '';
CALL DeleteAddress(@UserID,@AddressID,@AddressDeleted);
SELECT @AddressDeleted;

AddressDeleted根据记录是否删除正确返回(1或0)。

但是,如果我用C#代码阅读,它将返回1

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

                    oCommand.Parameters.AddWithValue("@UserID", UserID);
                    oCommand.Parameters.AddWithValue("@AddressID", AddressID);
                    MySqlParameter AddressDeleted = new MySqlParameter("@AddressDeleted", MySqlDbType.Int16);
                    AddressDeleted.Direction = System.Data.ParameterDirection.Output;
                    oCommand.Parameters.Add(AddressDeleted);

                    int result = oCommand.ExecuteNonQuery();
                    result = Convert.ToInt16(oCommand.Parameters["@AddressDeleted"].Value);

                    if (result > 0)
                    {
                        oDBStatus.Type = DBOperation.SUCCESS;
                        oDBStatus.Message.Add(DBMessageType.SUCCESSFULLY_DATA_DELETED);
                    }
                    else
                    {
                        oDBStatus.Type = DBOperation.ERROR;
                        oDBStatus.Message.Add(DBMessageType.ERROR_NO_RECORDS_UPDATED);
                    }
                    oMySQLConnecion.Close();
                    return oDBStatus;
                }

正如我提到的,如果我将零硬编码到存储过程中,它将返回该值。因此很可能存储过程出现了问题。

0 个答案:

没有答案