SQLServer数据库分页查询
public System.Collections.Generic.List<Model.HKSJ_Main> LoadPageData(int pageIndex, int pageSize, out int total)
{
DataSet ds = new DataSet();
SqlParameter totalParameter = new SqlParameter("@total", SqlDbType.Int);
totalParameter.Direction = ParameterDirection.Output;
//DbHelperSQL.RunProcedure()
//如果用了输出参数,那么就用SqlDataAdapter就可以了,用sqlDataReader时候拿不到输出参数的值。
using (SqlConnection conn = new SqlConnection(DbHelperSQL.connectionString))
{
//conn.Open();
using (SqlDataAdapter adapter = new SqlDataAdapter("P_LoadPageData", conn))
{
adapter.SelectCommand.Parameters.Add(new SqlParameter("@pageIndex", pageIndex));
adapter.SelectCommand.Parameters.Add(new SqlParameter("@pageSize", pageSize));
adapter.SelectCommand.CommandType = CommandType.StoredProcedure;
//输出参数的用法
adapter.SelectCommand.Parameters.Add(totalParameter);
adapter.Fill(ds);
}
}
total = (int)totalParameter.Value;//拿到输出参数的值
return this.DataTableToList(ds.Tables[0]);
}