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();
  • 相关阅读:
    【知识总结】Burnside 引理和 Polya 定理
    【洛谷1973】[NOI2011]NOI嘉年华(动态规划)
    【洛谷4705】玩游戏(多项式)
    【洛谷5366】[SNOI2017] 遗失的答案(状压DP)
    【Codeforces235D_CF235D】Graph Game(概率_基环树)
    【Codeforces553E_CF553E】Kyoya and Train(概率_CDQ分治_FFT)
    【知识总结】博弈论入门
    Saltstack
    Tomcat 的 catalina.out 日志分割
    eclipse的工程里的*.properties文件默认以unicode的编码形式显示
  • 原文地址:https://www.cnblogs.com/lihui1030/p/2869501.html
Copyright © 2011-2022 走看看