ADO.NET读取存储过程问题
下面是我写的存储过程:(此存储过程,在数据库里面执行完全正确)createprocProc_PagingQuery(@PageIndexint,@PageShowCoun...
下面是我写的存储过程:(此存储过程,在数据库里面执行完全正确)
create proc Proc_PagingQuery
(
@PageIndex int,
@PageShowCount int,
@PageCount int output
)
as
SELECT @PageCount=CEILING( COUNT(*)/CONVERT(float,@PageShowCount)) FROM Member;
SELECT * FROM ( SELECT ROW_NUMBER() OVER (ORDER BY Id DESC) AS RowNum,* FROM Member ) AS T
WHERE T.RowNum>(@PageIndex-1)*@PageShowCount AND T.RowNum<=@PageIndex*@PageShowCount;
go
下面是我写的ADO.NET调用存储过程的方法:
public static List<Member> Test(int pageIndex, int pageShowCount)
{
List<Member> list = new List<Member>();
using (SqlConnection conn = new SqlConnection(@"Data Source=.;Initial Catalog=FlightData;Integrated Security=True"))
{
using (SqlCommand cmd = new SqlCommand("Proc_PagingQuery", conn))
{
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter[] param = {
new SqlParameter("@PageIndex",pageIndex),
new SqlParameter("@PageShowCount",pageShowCount),
new SqlParameter("@PageCount",SqlDbType.Int)
};
param[2].Direction = ParameterDirection.Output;
cmd.Parameters.AddRange(param);
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
//获取输出参数时报错了,未将对象引用为实例,如果执行cmd.ExecuteNonQuery();下面一句代码就可以正确执行,为什么cmd.ExecuteReader()执行时就是错误的呢?
pageCount = cmd.Parameters["@PageCount"].Value.ToString();
if (dr.HasRows)
{
while (dr.Read())
{
list.Add(new Member()
{
Id = Convert.ToInt32(dr["Id"]),
Distributorid = Convert.ToInt32(dr["Distributorid"]),
LoginName = dr["LoginName"] as string,
RealName = dr["RealName"] as string,
LoginPwd = dr["LoginPwd"] as string
});
}
}
}
}
return list;
} 展开
create proc Proc_PagingQuery
(
@PageIndex int,
@PageShowCount int,
@PageCount int output
)
as
SELECT @PageCount=CEILING( COUNT(*)/CONVERT(float,@PageShowCount)) FROM Member;
SELECT * FROM ( SELECT ROW_NUMBER() OVER (ORDER BY Id DESC) AS RowNum,* FROM Member ) AS T
WHERE T.RowNum>(@PageIndex-1)*@PageShowCount AND T.RowNum<=@PageIndex*@PageShowCount;
go
下面是我写的ADO.NET调用存储过程的方法:
public static List<Member> Test(int pageIndex, int pageShowCount)
{
List<Member> list = new List<Member>();
using (SqlConnection conn = new SqlConnection(@"Data Source=.;Initial Catalog=FlightData;Integrated Security=True"))
{
using (SqlCommand cmd = new SqlCommand("Proc_PagingQuery", conn))
{
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter[] param = {
new SqlParameter("@PageIndex",pageIndex),
new SqlParameter("@PageShowCount",pageShowCount),
new SqlParameter("@PageCount",SqlDbType.Int)
};
param[2].Direction = ParameterDirection.Output;
cmd.Parameters.AddRange(param);
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
//获取输出参数时报错了,未将对象引用为实例,如果执行cmd.ExecuteNonQuery();下面一句代码就可以正确执行,为什么cmd.ExecuteReader()执行时就是错误的呢?
pageCount = cmd.Parameters["@PageCount"].Value.ToString();
if (dr.HasRows)
{
while (dr.Read())
{
list.Add(new Member()
{
Id = Convert.ToInt32(dr["Id"]),
Distributorid = Convert.ToInt32(dr["Distributorid"]),
LoginName = dr["LoginName"] as string,
RealName = dr["RealName"] as string,
LoginPwd = dr["LoginPwd"] as string
});
}
}
}
}
return list;
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询