C#调用存储过程实现分页(个人代码笔记)

分页的存储过程:
drop proc LoadPageMain 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 DAL层调用分页存储过程:  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;
        } aspx页面输出分页的方式:     在后台拼接字符串(数据输出的、分页连接的)输出到前台,后台通过前台传来的pageIndex、pageSize参数来动态改变数据  protected void Page_Load(object sender, EventArgs e)
        {
            int count = 0;
            int pageIndex = Request["pageIndex"]==null?1:Convert.ToInt32(Request["pageIndex"]);
            int pageSize = Request["pageSize"]==null?20:Convert.ToInt32(Request["pageSize"]);
   
            BLL.MyStudent bll_mystudent = new BLL.MyStudent();
            List<Model.MyStudent> modelList= bll_mystudent.getPagedList(pageIndex, pageSize, out count);             StringBuilder sb = new StringBuilder();
            for (int i = 0; i < modelList.Count; i++)
            {
                sb.Append(string.Format("<tr>  <td>" + modelList[i].FName + "</td> <td>" + modelList[i].FAge + "</td> <td>" + modelList[i].FGender + "</td> <td>" + modelList[i].FMath  + "</td> <td>" + modelList[i].FEnglish + "</td> <td>" + modelList[i].FClassId + "</td> <td>" + modelList[i].FBirthday + "</td> <td>修改 删除 详情</td>    </tr>"));
            }
            trBody = sb.ToString();             int pageCount=(count-1)/pageSize+1;
            StringBuilder sbFY = new StringBuilder();
            sbFY.Append("<a href='?pageSize=20&pageIndex=1'>上一页</a> ");
            for (int i = 0; i < pageCount; i++)
            {
                sbFY.Append(string.Format("<a href='?pageSize=20&pageIndex={0}'>{0}</a>&nbsp;&nbsp;&nbsp;", i, i));
            }
            sbFY.Append("<a href='?pageSize=20&pageIndex=1'>下一页</a> ");
            strFY = sbFY.ToString();
        }
上一篇:MS SQLServer 批量附加数据库 分类: SQL Server 数据库 2015-07-13 11:12 30人阅读 评论(0) 收藏


下一篇:第8章 装饰模式(Decorator Pattern)