C# 更新记录时,ExecuteNonQuery() 在 Update 中返回 -1

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2203100/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 00:22:57  来源:igfitidea点击:

ExecuteNonQuery() returns -1 in Update when records are updated

c#asp.netoracle11g

提问by Barryman9000

I've verified that my method/Oracle procedure is working correctly, but in my C# I always get -1 returned from ExecuteNonQuery(). Consequently, the below bool is always false. We only use Triggers in our DB (Oracle) for INSERT statements. Do I need a trigger for an update statement?

我已经验证我的方法/Oracle 过程工作正常,但在我的 C# 中,我总是从 ExecuteNonQuery() 返回 -1。因此,下面的布尔值始终为假。我们只在我们的数据库 (Oracle) 中将触发器用于 INSERT 语句。我需要更新语句的触发器吗?

Any suggestions why that would happen? Its definitely updating one record:

任何建议为什么会发生这种情况?它肯定会更新一条记录:

public bool ChangePassword(long UserId, string NewPassword)
    {
        int rcds = 0;
        using (OracleConnection dbConn = new OracleConnection(dbConnString))
        using (OracleCommand dbCmd = new OracleCommand("PKG_USER.CHANGE_PASSWORD", dbConn))
        {
            try
            {
                string salt = GenerateSalt();
                dbCmd.CommandType = CommandType.StoredProcedure;
                dbCmd.Parameters.Add("p_USER_ID", OracleDbType.Int64, UserId, ParameterDirection.Input);
                dbCmd.Parameters.Add("P_PASSWORD", OracleDbType.Varchar2, 128, EncodePassword(NewPassword, this.IsPasswordHashed, salt), ParameterDirection.Input);
                dbCmd.Parameters.Add("P_PASSWORD_SALT", OracleDbType.Varchar2, 128, salt, ParameterDirection.Input);

                if (dbConn.State != ConnectionState.Open) dbConn.Open();
                rcds = dbCmd.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                LastError = e.Message + " " + e.Source;
                rcds = 0;
            }
            finally
            {
                dbCmd.Dispose();
                dbConn.Dispose();
            }
        }
        return (rcds > 0);
    }

Sorry... heres the SP:

对不起......这是SP:

  PROCEDURE Change_Password(p_User_Id       IN Users.User_Id%TYPE,
                        p_Password      IN Users.Password%TYPE,
                        p_Password_Salt IN Users.Password_Salt%TYPE) IS


BEGIN
UPDATE Users
   SET Password             = p_Password,
       Password_Salt        = p_Password_Salt,
       Password_Change_Date = SYSDATE
 WHERE User_Id = p_User_Id;

END Change_Password;

END Change_Password;

采纳答案by MikeWyatt

Try explicitly returning SQL%ROWCOUNT.

尝试显式返回 SQL%ROWCOUNT。

According to MSDN, DbCommand..ExecuteNonQuery will always return -1 for stored procedure calls:

根据 MSDN,对于存储过程调用,DbCommand..ExecuteNonQuery 将始终返回 -1:

For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command. For all other types of statements, the return value is -1.

对于 UPDATE、INSERT 和 DELETE 语句,返回值是受命令影响的行数。对于所有其他类型的语句,返回值为 -1。

If I remember correctly from my days of using lots of stored procs, I believe you would need to use an output argument to return stuff like the number of updated rows.

如果我在使用大量存储过程的日子里没有记错的话,我相信您需要使用输出参数来返回诸如更新行数之类的东西。

回答by Steve Wortham

I'm not an Oracle guy, but apparently there's a command:

我不是 Oracle 人员,但显然有一个命令:

set feedback off

which prevents it from returning the count of records affected. Is this line in the stored procedure? Or have you tried 'set feedback on'? Functionally I think this is just the reverse of SQL Server's SET NOCOUNT ON/OFF command.

这会阻止它返回受影响的记录数。这条线在存储过程中吗?或者您是否尝试过“设置反馈”?从功能上讲,我认为这与 SQL Server 的 SET NOCOUNT ON/OFF 命令相反。