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;
        }
  • 相关阅读:
    [原]java开发文档的自动生成方式 java程序员
    帮助查看本地表单元素样子的网站 Native Form Elements java程序员
    Thingjs 开门示例:以3D机柜为例 演示thingjs如何开门
    基于WebGL架构的3D可视化平台—实现汽车行走路线演示
    WebGL的3D框架比较 ThingJS 和 Three.js
    重庆新闻联播 报道 thingJS 项目 反恐3D可视化预案 多警种3D可视化预案系统
    腾讯滨海大厦 智能楼宇 智慧建筑 3D可视化管理系统优锘科技ThingJS物联网开发案例
    基于WebGL架构的3D可视化平台—新风系统演示
    基于WebGL架构的3D可视化平台—三维设备管理(ThingJS实现楼宇设备管理3D可视化)
    【教程】ThingJS 3D开发快速入门 第一讲 开发概述·优势·项目流程
  • 原文地址:https://www.cnblogs.com/xbf321/p/895956.html
Copyright © 2011-2022 走看看