create Proc LoadPageMain
@pageIndex int,
@pageSize int,
@count int out
as
select top(@pageSize) * from dbo.MyStudent
where Fid not in
(
select top(@pageSize*(@pageIndex-1)) Fid
from dbo.MyStudent
order by Fid
)
order by Fid
select @count=COUNT(1) from dbo.MyStudent
public List<Model.MyStudent> getPagedList(int pageIndex, int pageSize, out int count)
{
List<Model.MyStudent> modelList = new List<Model.MyStudent>(); //创建最终要返回的model 的 List集合对象
string connStr = "server=.;database=CCDB;uid=sa;pwd=130988825";
using (SqlConnection conn = new SqlConnection(connStr))
{
using (SqlCommand cmd = new SqlCommand()) //创建command对象
{
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure; 设置command 对象的type属性
cmd.CommandText = "LoadPageMain"; //指定相应的存储过程
cmd.Parameters.Add(new SqlParameter("@pageIndex",pageIndex)); //为存储过程添加参数
cmd.Parameters.Add(new SqlParameter("@pageSize",pageSize));
SqlParameter outCount = new SqlParameter("@count",SqlDbType.Int); //创建将要输出的参数,并添加到参数集合中
outCount.Direction = ParameterDirection.Output;
cmd.Parameters.Add(outCount);
conn.Open();
using (SqlDataAdapter sda = new SqlDataAdapter(cmd)) //用SqlDataReader 对象回去数据,不用担心conn断开的问题
{
DataSet ds = new DataSet();
sda.Fill(ds); //获取存储过程返回的数据集
count = (int)outCount.Value; // 注意:获取 存储过程输出的参数值
foreach (DataRow row in ds.Tables[0].Rows )
{
Model.MyStudent model = new Model.MyStudent();
model.FName = Convert.ToString(row[1]);
model.FAge = Convert.ToInt32(row[2]);
model.FGender = Convert.ToString(row[3]);
model.FMath = row[4] is DBNull?null: (int?)Convert.ToInt32(row[4]);
model.FEnglish = Convert.ToInt32(row[5]);
model.FClassId = Convert.ToInt32(row[6]);
model.FBirthday = Convert.ToDateTime(row[7]);
modelList.Add(model);
}
}
}
}
return modelList;
}