最终返回的参数总是为空(我在delphi里执行,这个参数可以获取的),不知道是什么原因,望高手指点,代码如下:
public String ExecProc(string procName, SqlParameter[] param_in, SqlParameter[] param_out)
        {
            try
            {
                this.cmd.CommandType = CommandType.StoredProcedure;
                this.cmd.CommandText = procName;
                this.cmd.Parameters.Clear();
                if (param_in != null)
                {
                    foreach (SqlParameter param in param_in)
                    {
                        param.Direction = ParameterDirection.Input;
                        this.cmd.Parameters.Add(param);
                    }
                }
                if (param_out != null)
                {
                    foreach (SqlParameter param in param_out)
                    {
                        param.Direction = ParameterDirection.Output;
                        this.cmd.Parameters.Add(param);
                    }
                }
                this.cmd.ExecuteNonQuery();
                return param_out[0].Value.ToString();
            }
            catch (Exception e)
            {
                throw e;
            }
        }

解决方案 »

  1.   

    你的存储过程有那么多output输出参数么…
      

  2.   

    你用sql server的profile跟踪下,看是否返回了参数,首先确定存储过程是否写对
      

  3.   

    //以下方法仅供参考
    //_SqlServerDB.Command 为SqlCommand数据类型
    //_SqlServerDB.OpenDB 为封装打开数据库连接方法
    //_SqlServerDB.CloseDB 为封装关闭数据连接方法[/align]   public int InsertDB()
            {
                string strSql;
                int iRet;
                int iReturnValue;            _SqlServerDB.OpenDB();//打开数据库连接
                _SqlServerDB.Command.Parameters.Clear();
                _SqlServerDB.Command.Parameters.Add("@i_group_id", SqlDbType.Int).Value = 1;
                _SqlServerDB.Command.Parameters.Add("@i_device_id", SqlDbType.Int).Value = 2;
                _SqlServerDB.Command.Parameters.Add("@o_id", SqlDbType.Int);
                _SqlServerDB.Command.Parameters["@o_id"].Direction = ParameterDirection.Output;
                iRet = _SqlServerDB.ExecProcStore("p_insert_data");
                if (iRet == 0)
                    iReturnValue = Convert.ToInt32(_SqlServerDB.Command.Parameters["@o_id"].Value);
                else
                    iReturnValue = -1;
                _SqlServerDB.CloseDB();//关闭数据库连接
                return iRet;
            }        public int ExecProcStore(string strProcName)
            {
                int iRet = 0;
                if (_Connected == -1)
                    return -1;
                try
                {
                    _Command.CommandType = CommandType.StoredProcedure ;
                    _Command.CommandText = strProcName;
                    iRet = _Command.ExecuteNonQuery();
                    if (iRet > 0)
                        return 0;
                    else
                        return -1;
                }
                catch (SqlException err)
                {
                    Console.WriteLine(err.Message);
                    Console.WriteLine(strProcName);
                    return -1;
                }
                catch (Exception err)
                {
                    Console.WriteLine(err.Message);
                    Console.WriteLine(strProcName);
                    return -1;
                }
            }祝好运
      

  4.   

    知道原因了,因为我传入参数时赋值了,所以需要用InputOutput,这样就可以接受到返回值了,谢谢各位了!!