日期:2014-05-19  浏览次数:21086 次

ExecuteNonQuery()方法执行存储过程,获取RETURN_VALUE返回值的问题!
我用ExecuteNonQuery()执行存储过程获取不到返回值   都是在执行的时候出错   错误信息是 "将参数值从   String   转换到   Int32   失败。 "存储过程在SQL中执行没有问题!
查了好多资料   方法基本与我的差不多   有人能帮下忙不!

以下是代码
        public   int   Execute(string   spname,   string   usrid,   string   pwd)
        {
                int   ret;

                _command.CommandText   =   spname;
                _command.CommandType   =   CommandType.StoredProcedure;
                _command.Connection   =   _connection;

                _command.Parameters.Add( "@RETURN_VALUE ",   SqlDbType.Int);
                _command.Parameters[ "@RETURN_VALUE "].Value   =   " ";
                _command.Parameters[ "@RETURN_VALUE "].Direction   =   ParameterDirection.ReturnValue;
                _command.Parameters.Add( "@UserID ",   SqlDbType.VarChar);
                _command.Parameters[ "@UserID "].Value   =   usrid;
                _command.Parameters[ "@UserID "].Direction   =   ParameterDirection.Input;
                _command.Parameters.Add( "@PassWord ",   SqlDbType.VarChar);
                _command.Parameters[ "@PassWord "].Value   =   pwd;
                _command.Parameters[ "@PassWord "].Direction   =   ParameterDirection.Input;

                _connection.Open();
                ret   =   _command.ExecuteNonQuery();

                ret   =   (int)_command.Parameters[ "@RETURN_VALUE "].Value;

                return   ret;
        }
spname是存储过程,另外两个是传过来的参数   数据库连接在另外个方法里   连接没问题!


------解决方案--------------------
_command.Parameters.Add( "@RETURN_VALUE ", SqlDbType.Int);
_command.Parameters[ "@RETURN_VALUE "].Value = " ";