页面载入中......
首页 联系我 返回顶部
同样的参数去执行一段存储过程
返回类型为DataTable时有值,而返回SqlDataReader时却为空.
public static DataTable ExecuteDataTable(string Text, CommandType commandType, SqlParameter[] para, out int pagecount, out int recordcount)
        {
            using (SqlConnection conn = new SqlConnection(SqlConnectionString))
            {
                SqlCommand comm = conn.CreateCommand();
               
                comm.CommandText = Text;
                comm.CommandType = commandType;
                foreach (SqlParameter p in para)
                {
                    comm.Parameters.Add(p);

                }
                comm.Parameters.Add("@pagecount", SqlDbType.Int);
                comm.Parameters.Add("@RecordCount", SqlDbType.Int);
                comm.Parameters["@pagecount"].Direction = ParameterDirection.Output;
                comm.Parameters["@RecordCount"].Direction = ParameterDirection.Output;
                SqlDataAdapter da = new SqlDataAdapter(comm);
                DataSet dst = new DataSet();
                da.Fill(dst);
                if (conn.State == ConnectionState.Open)
                    conn.Close();
                pagecount = (int)comm.Parameters["@pagecount"].Value;  //这里返回正确
                recordcount = (int)comm.Parameters["@RecordCount"].Value;   //这里也正确
                return dst.Tables[0];
            }
        }
返回类型为SqlDataReader时
public static SqlDataReader ExecuteDataReader(string Text, CommandType commandType, SqlParameter[] para, out int pagecount, out int recordcount)
        {
            pagecount = 1;
            recordcount = 0;
            using (SqlConnection conn = new SqlConnection(SqlConnectionString))
            {
                SqlCommand comm = conn.CreateCommand();

                comm.CommandText = Text;
                comm.CommandType = commandType;
                foreach (SqlParameter p in para)
                {
                    comm.Parameters.Add(p);

                }
                comm.Parameters.Add("@PageCount", SqlDbType.Int);
                comm.Parameters.Add("@RecordCount", SqlDbType.Int);
                comm.Parameters["@PageCount"].Direction = ParameterDirection.Output;
                comm.Parameters["@RecordCount"].Direction = ParameterDirection.Output;
                conn.Open();
                using (SqlDataReader dr = comm.ExecuteReader())
                {
                    if (dr.Read())
                    {
                        pagecount = (int)comm.Parameters["@pagecount"].Value; //null
                        recordcount = (int)comm.Parameters["@recordcount"].Value;   //null
                    }
                    return dr;
                }
            }
        }
为什么会这样呢?

原因:

当您将 Command 对象用于存储过程时,可以将 Command 对象的 CommandType 属性设置为 StoredProcedure。当 CommandType 为 StoredProcedure 时,可以使用 Command 的 Parameters 属性来访问输入及输出参数和返回值。无论调用哪一个 Execute 方法,都可以访问 Parameters 属性。但是,当调用 ExecuteReader 时,在 DataReader 关闭之前,将无法访问返回值和输出参数。

ref:http://msdn2.microsoft.com/zh-CN/library/tyy0sz6b.aspx

修改了一下:

 SqlDataReader dr = comm.ExecuteReader(CommandBehavior.CloseConnection);
                dr.NextResult();   //加了这一行
                pagecount = (int)comm.Parameters["@pagecount"].Value;
                recordcount = (int)comm.Parameters["@recordcount"].Value;
                
                    return dr;
                }

这样就行了

Write a comment:



(将显示你的Gravatar图标)  



[b][/b] - [i][/i] - [u][/u]- [quote][/quote]

:-/ ^_^ :d :o :kiss: :) :p :se: [yeah] :( :love: :han: :up: :cry: :zzz: o_o

暂时停止交换链接.

评论提交有问题?