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;
}
}
参考的datalist分页helper
最新推荐文章于 2022-09-17 11:53:26 发布