参考的datalist分页helper

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Web;  
using System.Web.UI;  
using System.Web.UI.WebControls;  
using System.Data.SqlClient;  
using System.Data;  
/// <summary>   
///Connection 的摘要说明   
/// </summary>   
public class SqlHelper  
{  
    public SqlConnection connect()//连接数据库   
    {  
        SqlConnection con = new SqlConnection(@"Data Source=.\SQL2005;Initial Catalog=school;Integrated Security=True");  
        con.Open();  
        return con;  
    }  
    public SqlDataReader sdr(string sqlstr)//DataReader读取   
    {  
        SqlCommand cmd = new SqlCommand(sqlstr, connect());  
        SqlDataReader dr = cmd.ExecuteReader();  
        return dr;  
    }  
    public DataList dl(string sqlstr,DataList DataListName)//将DataList与DataReader绑定   
    {  
        DataListName.DataSource = sdr(sqlstr);  
        DataListName.DataBind();  
        sdr(sqlstr).Close();  
        connect().Close();  
        return DataListName;  
    }  
    public DataSet datas(string sqlstr)  
    {  
        SqlDataAdapter sda = new SqlDataAdapter(sqlstr,connect());  
        DataSet dd = new DataSet();  
        sda.Fill(dd);  
        connect().Close();  
        return dd;  
    }  
    public void NewsScroll(string sqlstr,int i, ImageButton IB,HyperLink HL,string PostBackUrl)  
    {  
        DataSet ds=datas(sqlstr);  
        IB.ImageUrl = ds.Tables[0].Rows[i]["NewsImage"].ToString();  
        IB.PostBackUrl = PostBackUrl + "?Id=" + ds.Tables[0].Rows[i]["Id"].ToString();  
        HL.Text = ds.Tables[0].Rows[i]["NewsTitle"].ToString();  
        HL.NavigateUrl = IB.PostBackUrl;  
    }  
    public PagedDataSource ContentPaging(string sqlstr,int PageSize,int CurrentPageIndex,Label CurrentPageIndexLabel, Label DataSourceCount,Label PageCount,DataList DataListName)//内容分页函数   
    {  
        //PagedDataSource和DataSet绑定   
        PagedDataSource pds=new PagedDataSource();  
        pds.DataSource = datas(sqlstr).Tables[0].DefaultView;  
        pds.AllowPaging = true;  
        pds.PageSize = PageSize;  
        pds.CurrentPageIndex = CurrentPageIndex-1;  
        //   
        CurrentPageIndexLabel.Text = CurrentPageIndex.ToString();  
        DataSourceCount.Text = pds.DataSourceCount.ToString();  
        PageCount.Text = pds.PageCount.ToString();  
        //DataList和PagedDataSource绑定   
        DataListName.DataSource = pds;  
        DataListName.DataBind();  
        return pds;  
    }  
}  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值