/*定義一個datatable dt */
SqlConnetion conn =creatConn();
SqldataAdapter Adapter = new SqlDataAdapter(mySqlCode,conn);
ds = new DataSet();
/*****************************添加標識列***************************************/
DataTable dt = new DataTable("test");
DataColumn dc = dt.Columns.Add("pid",typeof(int));
dc.AutoIncrement = true;
dc.AutoIncrementSeed = 1;
dc.AutoIncrementStep = 1;
ds.Tables.Add(dt);
/*****************************添加標識列***************************************/
Adapter.Fill(ds,"test");
dataGrid1.SetDataBinding(ds,"test");
/****************************實現翻頁*****************************************/
int rows = ds.Tables["test"].Rows.Count;
if (rows % pagerows == 0)
{
totalpages = rows / pagerows;
}
else
{
totalpages = rows / pagerows + 1;
}
beginid = 1;
endid = beginid + pagerows;
//篩選
DataView dv = ds.Tables["test"].DefaultView;
dv.RowFilter = "pid> ="+beginid +"and pid <="+endid;
dataGrid1.DataSource = dv;
cupages=1;
this.label1.Text = "當前頁爲:" + cupages;
/*下一頁* /
if (cupages < totalpages)
{
beginid = endid;
endid = beginid + pagerows;
DataView dv = ds.Tables["test"].DefaultView;
dv.RowFilter = "pid> =" + beginid + "and pid <=" + endid;
//dataGrid1.DataSource = dv;
cupages++;
this.label1.Text = "當前面爲:"+cupages;
}
/*上一頁* /
if (cupages> 1)
{
endid = beginid;
beginid = beginid - pagerows;
DataView dv = ds.Tables["test"].DefaultView;
dv.RowFilter = "pid> ="+beginid+"and pid <="+endid;
//dataGrid1.DataSource = dv;
cupages--;
this.label1.Text = "當前面爲:"+cupages;
}
/*獲得當前列出的id數組的sql語句*/
//第一種
SELECT * FROM (
SELECT TOP @PageSize * FROM (
SELECT TOP @PageSize*(@PageIndex+1) * FROM @TableName
ORDER BY @PrimaryKey ASC
) TableA ORDER BY @PrimaryKey DESC
) TableB ORDER BY @PrimaryKey ASC
//第二種
SELECT TOP @PageSize * FROM @TableName
WHERE @PrimaryKey NOT IN (
SELECT TOP @PageSize*@PageIndex @PrimaryKey FROM @TableName
ORDER BY @PrimaryKey ASC
) ORDER BY @PrimaryKey ASC