DBaction db = new DBaction();
string sql1 = "select * from adlist where ad_top ='Y'";
string sql2 = "select * from adlist where ad_top ='N'";
DataTable dt1 = db.Select(sql1);
DataTable dt2 = db.Select(sql2);
DataTable dt = new DataTable();
dt = dt2;
dt.Merge(dt1);//将dt2接入dt1变成dt。
DataSet ds = new DataSet();
ds.Tables.Add(dt); sqlDataAdapter sda = new OleDbDataAdapter("select * from t_product",conn);//省略了连接过程代码
DataSet ds = new DataSet();
sda.Fill(ds, pager1.PageSize * (pager1.CurrentPageIndex - 1), pager1.PageSize, "temptbl");
DataList1.DataSource = ds.Tables["temptbl"];
DataList1.DataBind();我想请问如何将第一段代码中的DataTable dt能像第二段代码中的“sda.Fill(ds, pager1.PageSize * (pager1.CurrentPageIndex - 1), pager1.PageSize, "temptbl");”这样,截取一段放入dataset?因为我要分页。
ps:因为需要我要执行两次select,请各位不要建议我只执行一次select,然后用sqlDataAdapter。
{
SqlConnection cn = new SqlConnection("Data Source=.;Initial Catalog=Test;Integrated Security=True");//连接并实例化数据库
string sql = "select * from Student";//定义查询语句
SqlDataAdapter da = new SqlDataAdapter(sql, cn);//实例化对象Adapter
DataSet ds = new DataSet();//实例化DataSet
da.Fill(ds, "Student");//填充 PagedDataSource pds = new PagedDataSource();//初始化分页事例
pds.DataSource = ds.Tables["Student"].DefaultView;
pds.AllowPaging = true;//启动分页
pds.PageSize = 5;//每页显示的个数
CurrentIndex = int.Parse(this.Label1.Text) - 1;//获取当前页数索引
pds.CurrentPageIndex = CurrentIndex;
if (CurrentIndex == 0)
{//如果是第一页,上一页和第一页的控件不可点击
this.PreviousLB.Enabled = false;
this.FirstLB.Enabled = false;
this.NextLB.Enabled = true;
this.EndLB.Enabled = true;
}
else if (CurrentIndex == pds.PageCount - 1)
{
//如果是最后一页,下一页和最后一页空间不可点击
this.PreviousLB.Enabled = true;
this.FirstLB.Enabled = true;
this.NextLB.Enabled = false;
this.EndLB.Enabled = false; }
else
{
this.PreviousLB.Enabled = true;
this.FirstLB.Enabled = true;
this.NextLB.Enabled = true;
this.EndLB.Enabled = true; }
this.Label2.Text = pds.PageCount.ToString();//获取总页数
DataList1.DataSource = pds;//绑定DataList数据
DataList1.DataBind();
}
protected void FirstLB_Click(object sender, EventArgs e)//首页
{
this.Label1.Text = "1";//页数为1
DataBinds();
}
protected void PreviousLB_Click(object sender, EventArgs e)
{
this.Label1.Text = (int.Parse(Label1.Text) - 1).ToString();//页数减1
DataBinds();
}
protected void NextLB_Click(object sender, EventArgs e)//下一页
{
this.Label1.Text = (int.Parse(this.Label1.Text) + 1).ToString();//页数加1
DataBinds(); }
protected void EndLB_Click(object sender, EventArgs e)//末页
{
this.Label1.Text = Label2.Text;//页数为最后一页
DataBinds();
}
protected void JumpLB_Click(object sender, EventArgs e)
{ try
{ if (int.Parse(TextBox1.Text) > 0 && int.Parse(TextBox1.Text) <= int.Parse(Label2.Text))
{
this.Label1.Text = TextBox1.Text;
DataBinds();
}
else
{
Response.Write("<script>alert('请输入有效数字')</script>");
TextBox1.Text = null;
}
}
catch
{
Response.Write("<script>alert('系统出错')</script>");
Response.Redirect("~/Default.aspx");
}
}
string sql1 = "select * from adlist where ad_top ='Y'";
string sql2 = "select * from adlist where ad_top ='N'";
//既然下面总归要把DataTable合并的,那sql语句不这样写?
string sql1 = "select * from adlist where ad_top ='Y' or ad_top ='N' ";
BoundControl.DataSource = dt.Skip((CurrentPageIndex - 1) * PageSize).Take(PageSize);
BoundControl.DataBind(); 你这个需求 就是要做一个假分页
假分页 用Linq在适合不过了。
framework 2.0 的话 可以去下一个 LinqBridge