zoukankan      html  css  js  c++  java
  • sql+aspnetpager+查询功能

    分页代码:
    View Code
    #region 通过SQL语句分页
    
            public DataSet GetPageDataBySql(int pageIndex, int pageSize, string tbName, string tbID, string keyName,string keyword,string orderType)
            {
                string strConn = ConfigurationManager.ConnectionStrings["DB"].ConnectionString;
    
                SqlConnection conn = new SqlConnection(strConn);
    
                try
                {
                    conn.Open();
    
                    SqlCommand cmd = conn.CreateCommand();
    
                    string sql = "";
    
                    cmd.CommandType = CommandType.Text;
                    if (!string.IsNullOrEmpty(keyword) && !string.IsNullOrEmpty(keyName))
                    {
                        sql = "select top " + pageSize + " * from [" + tbName + "] where " + tbID + " not in(select top " + (pageIndex - 1) * pageSize + " " + tbID + " from [" + tbName + "]  order by " + tbID + " "+orderType+") and " + keyName + " like '%" + keyword + "%'  order by " + tbID + " "+orderType;
                    }
                    else
                    {
                        sql = "select top " + pageSize + " * from [" + tbName + "] where " + tbID + " not in(select top " + (pageIndex - 1) * pageSize + " " + tbID + " from [" + tbName + "]  order by " + tbID + " " + orderType + ")   order by " + tbID + " " + orderType;
                    }
    
    
                    cmd.CommandText = sql;
    
                    SqlDataAdapter ada = new SqlDataAdapter(cmd);
                    DataSet ds = new DataSet();
                    DataTable dt = new DataTable();
    
                    if (ada != null)
                    {
                        ada.Fill(ds);
                        return ds;
                    }
                    return null;
                }
                finally
                {
                    conn.Close();
                }
    
            }  
            #endregion
    获取总数:
    View Code
    #region 根据条件查找数据的总数
            public static int GetCount(string tbName,string keyName,string keyword)
            {
                string strConn = ConfigurationManager.ConnectionStrings["DB"].ConnectionString;
    
                SqlConnection conn = new SqlConnection(strConn);
    
                try
                {
                    conn.Open();
    
                    string sql = "";
    
                    SqlCommand cmd = conn.CreateCommand();
    
                    if (!string.IsNullOrEmpty(keyName) && !string.IsNullOrEmpty(keyword))
                    {
                        sql = "select * from " + tbName+ "  where  "+keyName+" like '%"+keyword+"%'";
                    }
                    else
                    {
                        sql = "select * from " + tbName;
                    }
    
                    cmd.CommandText = sql;
                    SqlDataAdapter ada = new SqlDataAdapter(cmd);
                    DataSet ds = new DataSet();
                    DataTable dt = new DataTable();
                    int Count = 0;
                    if (ada != null)
                    {
                        ada.Fill(ds);
                        dt = ds.Tables[0];
                        return Count = dt.Rows.Count;
                    }
                    return 0;
                }
                finally
                {
                    conn.Close();
                }
    
            }
            #endregion
    调用代码:

    public int pageSize = 50;//一页显示50条数据 public string keyword = string.Empty;//查找的关键字 public int term = 1;//根据类别查找,默认【按名字】 public string orderName ="ID";//根据这字段排序,默认【ID】 public string orderType = "desc";//排序的方式,默认【降序】 string tbName = "tb_viewproduct";//表名 AspNetPager1.PageSize = pageSize; ds = pg.GetPageDataBySql(AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, tbName, orderName, keyName, keyword, orderType); AspNetPager1.RecordCount = Pager.GetCount(tbName, keyName, keyword); lb1.Text = AspNetPager1.RecordCount.ToString(); Repeater1.DataSource = ds; Repeater1.DataBind();
  • 相关阅读:
    关于sifari兼容性的一个问题
    HTML标签的应用(新手)
    HTML标签的应用(新手)
    未完成的开锁动画演示
    HTML标签的应用(新手)
    HTML新手向
    C++
    STL之set
    C++输入输出
    提升一下逼格
  • 原文地址:https://www.cnblogs.com/lihui1030/p/2869501.html
Copyright © 2011-2022 走看看