public DataTable VoteItemsShow(string ID,out string count)
        {
            SqlParameter[] para = new SqlParameter[]{
                new SqlParameter("@ID",ID),
                new SqlParameter("@count",ParameterDirection.Output) //这个@count是存储过程中的输出参数,我右边的参数应该是写错了,,因为调用这个函数返回的是Output字符,而不是@count应该怎么写呢?
            };      
            DataTable dt = sqlhelper.ExecuteQuery("procVoteItemsShow", para, CommandType.StoredProcedure,out count);
            return dt;
        }
这个是被调用的函数,SqlHelper public DataTable ExecuteQuery(string cmdText, SqlParameter[] para, CommandType ct,out string count)
        {
            DataTable dt = new DataTable();
            try
            {
                cmd = new SqlCommand(cmdText, GetConn());
                cmd.Parameters.AddRange(para);
                cmd.CommandType = ct;
                SqlDataReader sdr = cmd.ExecuteReader();
                count = cmd.Parameters["@count"].Value.ToString(); //这里接收@count的值可否?为什么这个count变量返回的是Output字符?,,,
                dt.Load(sdr);
                sdr.Close();
            }
            catch (Exception ex)
            {                throw ex;
            }
            finally
            {
                if (conn.State == ConnectionState.Open) conn.Close();
            }
            return dt;
        }

解决方案 »

  1.   

     param = new OleDbParameter("@ID", OleDbType.Integer);
                param.Direction = ParameterDirection.InputOutput;
                list.Add(param);
      

  2.   

    count = cmd.Parameters["@count"].Value.ToString(); //这里接收@count的值可否?为什么这个count变量返回的是Output字符?,,,
      dt.Load(sdr);
      sdr.Close();=>
      dt.Load(sdr);
      sdr.Close();
    count = cmd.Parameters["@count"].Value.ToString();
      

  3.   

    2楼的不行啊,,还是一样,,应该是这个存储过程参数new SqlParameter("@count",ParameterDirection.Output)出了问题,,1楼的话,,不能改成以数组形式吗?
      

  4.   

    判断value值
    public static SqlParameter MakeParam(string ParamName, SqlDbType DbType, Int32 Size, ParameterDirection Direction, object Value)
            {
                SqlParameter param;            if (Size > 0)
                    param = new SqlParameter(ParamName, DbType, Size);
                else
                    param = new SqlParameter(ParamName, DbType);            param.Direction = Direction;
                if (!(Direction == ParameterDirection.Output && Value == null))
                    param.Value = Value;            return param;
            }
         /// <summary>
            /// 传入返回值参数
            /// </summary>
            /// <param name="ParamName">存储过程名称</param>
            /// <param name="DbType">参数类型</param>
            /// <param name="Size">参数大小</param>
            /// <returns>新的 parameter 对象</returns>
            public static SqlParameter MakeOutParam(string ParamName, SqlDbType DbType, int Size)
            {
                return MakeParam(ParamName, DbType, Size, ParameterDirection.Output, null);
            }
      

  5.   

    选谢过大家,,我主要是想知道以数组形式写的output参数,,单个参数的网上很都是,,SqlParameter[] para = new SqlParameter[]{
      new SqlParameter("@ID",ID),
      new SqlParameter("@count",ParameterDirection.Output) //这个是不是这样写呀?
      };
    如果不是,那要怎么写呢? 
      

  6.   

    SqlParameter[] para = new SqlParameter[]{
      new SqlParameter("@ID",ID),
      new SqlParameter("@count",ParameterDirection.Output) 
    }; =>SqlParameter[] para = new SqlParameter[]{
      new SqlParameter("@ID",ID),
      new SqlParameter("@count",count) 
    };
    para[1].Direction = ParameterDirection.Output;
      

  7.   

    new SqlParameter[]{
      new SqlParameter("@ID",ID),
      new SqlParameter("@count",count)   
    };
    para[1].Direction = ParameterDirection.Output;
      

  8.   

    SqlParameter[] para = new SqlParameter[]{
      new SqlParameter("@ID",ID),
      new SqlParameter("@count",count)  
    };
    para[1].Direction = ParameterDirection.Output;
    改成这个后都提示“未将对象引用设置到对象的实例。”不知道哪错了,,而且output的参数应该不用传入的吧,,?
      

  9.   

    SqlParameter[] para = new SqlParameter[]{
      new SqlParameter("@ID",ID),
      new SqlParameter("@count",count)   
    };
    para[1].Direction = ParameterDirection.Output;
    給它個null值。。
      

  10.   

    这个count是out string count函数传出的参数呀,,  用这个后new SqlParameter("@count",count)  提示使用了未赋值的参数,
      

  11.   

    那你看看你的count賦值了沒有。。
      

  12.   


    SqlParameter[] para = new SqlParameter[]{
      new SqlParameter("@ID",ID),
      new SqlParameter("@count",null)  
    };
    para[1].Direction = ParameterDirection.Output;
      

  13.   

    当count= null时提示,,。。异常详细信息: System.InvalidOperationException: String[1]: Size 属性具有无效大小值 0。
    在这个调用函数中没有赋值呀,,值在Sqlhelper类中赋了,,用output传出的呀,,
    count = cmd.Parameters["@count"].Value.ToString();
      

  14.   


    SqlParameter[] parameters = {
    new SqlParameter("@pageIndex", SqlDbType.Int),
    new SqlParameter("@pageSize", SqlDbType.Int),
                        new SqlParameter("@sqlStr", SqlDbType.NVarChar,800),
                        new SqlParameter("@orderFiled", SqlDbType.VarChar,50),
                        new SqlParameter("@recordsCount", SqlDbType.Int),
    };
                parameters[0].Value = pageIndex;
                parameters[1].Value = pageSize;
                parameters[2].Value = sqlStr;
                parameters[3].Value = orderFiled;
                parameters[4].Direction = ParameterDirection.Output;
      

  15.   

    new SqlParameter("@count",null)异常详细信息: System.InvalidOperationException: String[1]: Size 属性具有无效大小值 0。
      

  16.   

     SqlParameter[] para = new SqlParameter[]{
                    new SqlParameter("@ID",SqlDbType.Int),
                    new SqlParameter("@count",SqlDbType.Int)            };
                para[0].Value = ID;
                para[1].Direction = ParameterDirection.Output;
    提示System.NullReferenceException:未将对象引用设置到对象的实例,我第一次使用output来输出参数,不懂呀,,不知道哪错了,,……
      

  17.   

    CREATE procedure procVoteItemsShow
    @ID int,
    @count int = 0 output
    as
    begin
    select sum([Count]) from Vote where Sid = @ID
    select  ID,Title,[Count],Date from Vote where Sid = @ID
    end
    GO
    这个是存储过程
      

  18.   

    SqlParameter[] para = new SqlParameter[]{
      new SqlParameter("@ID",ID),
      new SqlParameter("@count",SqlDbType.Int)
      };
      para[1].Direction = ParameterDirection.Output;
      

  19.   

    SqlParameter[] para = new SqlParameter[]{
      new SqlParameter("@ID",ID),
      new SqlParameter("@count",SqlDbType.Int)
      };
      para[1].Direction = ParameterDirection.Output;
    辛苦大家了,,这个调用时Sqlhelper类里还是抛出异常,提示System.NullReferenceException: 未将对象引用设置到对象的实例。这一般是什么错误呢,我是新手,如果那位方便的话我把文件发过来,帮我改改可以否?
      

  20.   

    count = cmd.Parameters["@count"].Value.ToString();,,改成count = cmd.Parameters[1].Value.ToString()也是一样,未将对象引用设置到对象的实例
      

  21.   

    位置要放在sdr.Close();后面count = (cmd.Parameters["@count"].Value??"").ToString();
      

  22.   

    OK了,,不过我想顺便问下为什么count = (cmd.Parameters["@count"].Value??"").ToString();要放在sdr.Close();后面呢?不是执行 SqlDataReader sdr = cmd.ExecuteReader();后它的值就已经返回了吗?
      

  23.   

    ExecuteReader 在数据库连接没有关闭前是没有返回值的http://topic.csdn.net/u/20091210/17/D212FFC1-45E1-4D96-9A1C-7CC5437E929E.html