zoukankan      html  css  js  c++  java
  • 一个用泛型,和Sql语句分页的源代码

       
    protected void Page_Load(object sender, EventArgs e)
        {
            
    if (!Page.IsPostBack)
                BindDataGridView();
        }

        
    private void BindDataGridView() 
        {
            
    this.ProductGridView.DataSource = CreateDataSource();
            
    this.ProductGridView.DataBind();
        }

        
    private IList<ProductInfo> CreateDataSource()
        {
            
    int totalRecords = 0;
            IList
    <ProductInfo> products = GetProducts(Pager1.CurrentPageIndex, Pager1.PageSize, out  totalRecords);
            
    this.Pager1.TotalRecords = totalRecords;
            
    return products;
        }

        
    private IList<ProductInfo> GetProducts(int pageIndex,int pageSize,out int totalRecords)
        {
            
    string conenctionString = "Data Source=.;Initial Catalog=Northwind;Integrated Security=True";
            StringBuilder sb 
    =new StringBuilder();
            sb.Append(
    " DECLARE @PageLowerBound int  DECLARE @PageUpperBound int ");
            sb.Append(
    " SET @PageLowerBound = @PageSize * @PageIndex  SET @PageUpperBound = @PageLowerBound + @PageSize - 1 ");
            sb.Append(
    " CREATE TABLE #PageIndex(Id   int  IDENTITY(0,1) NOT NULL, ProductID int) ");
            sb.Append(
    " INSERT #PageIndex(ProductID) " + 
                      
    " SELECT ProductID "+
                      
    " FROM Products " +
                      
    " ORDER BY ProductID " );
            sb.Append(
    " SELECT @TotalRecords = @@ROWCOUNT ");
            sb.Append(
    " SELECT pt.CategoryID, pt.ProductID,pt.ProductName "+
                      
    " FROM #PageIndex p,Products pt "+
                      
    " WHERE p.ProductID =pt.ProductID AND "+
                      
    " p.Id >= @PageLowerBound AND p.Id <= @PageUpperBound "+
                      
    " ORDER BY pt.ProductID ");
            List
    <ProductInfo> products = new List<ProductInfo>();
            
    using (SqlConnection cnn = new SqlConnection(conenctionString))
            {
                SqlCommand cmd 
    = new SqlCommand(sb.ToString(), cnn);
                cmd.CommandType 
    = CommandType.Text;

                cmd.Parameters.Add(
    new SqlParameter("@PageIndex", SqlDbType.Int));
                cmd.Parameters[
    "@PageIndex"].Value = pageIndex;
                cmd.Parameters.Add(
    new SqlParameter("@PageSize", SqlDbType.Int));
                cmd.Parameters[
    "@PageSize"].Value = pageSize;
                cmd.Parameters.Add(
    new SqlParameter("@TotalRecords", SqlDbType.Int));
                cmd.Parameters[
    "@TotalRecords"].Direction = ParameterDirection.Output;
                cnn.Open();
                
    using (SqlDataReader dr = cmd.ExecuteReader())
                {
                    
    while (dr.Read())
                        products.Add(
    new ProductInfo(dr.GetInt32(0), dr.GetInt32(1), dr.GetString(2)));
                }
                totalRecords 
    = (int)cmd.Parameters["@TotalRecords"].Value;
            }
            
    return products;
        }
  • 相关阅读:
    大数据第59天—MySQL之员工奖金-杨大伟
    大数据第58天—MySQL常用命令-杨大伟
    大数据第56天—Mysql练习题12道之十一-查出销售表中所有会员购买金额,同时分组查出退货表中所有会员的退货金额-杨大伟
    大数据第55天—Mysql练习题12道之十-查询各自区组的money排名前十的账号-杨大伟
    大数据第54天—Mysql练习题12道之九-充值日志表-杨大伟
    大数据第53天—Mysql练习题12道之八-线上服务器访问日志-杨大伟
    大数据第52天—Mysql练习题12道之七-图书管理数据库-杨大伟
    mac 破解pycharm2020.2.3
    mac连接oracle数据库
    python DPI-1047:Cannot locate a 64-bit Oracle Client library:The specified module could not be found.
  • 原文地址:https://www.cnblogs.com/xbf321/p/895956.html
Copyright © 2011-2022 走看看