C#调用存储过程实现分页

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;
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值