方法一: 把你要取的数据按某一字段排序,然后生成一个顺序ID, 可以写成存储过程形式, 例: use pubs declare @fromid int declare @toid int select @fromid=10,@toid=20 select c.* from authors c, (select a.au_id,count(a.au_id) as id_no from authors a,authors b where a.au_id>b.au_id group by a.au_id) d where c.au_id=d.au_id and id_no between @fromid and @toid 方法二: 先把数据存到一个#table1中然后生成一ID_NO,然后依ID_NO取想要的数据 例: use pubs declare @fromid int declare @toid int select @fromid=10,@toid=20 if object_id('tempdb..#table1') is not null drop table tempdb..#table1 select IDENTITY(int, 1,1) AS ID_NO,* into #table1 from authors select * from #table1 where id_no between @fromid and @toid
这是书上的代码,但是并不能正确执行,请各位看看如何改?protected System.Web.UI.WebControls.DataGrid DataGrid1; SqlConnection objConnection; string Select; int intStartIndex; int intEndIndex;
把你要取的数据按某一字段排序,然后生成一个顺序ID,
可以写成存储过程形式,
例:
use pubs
declare @fromid int
declare @toid int
select @fromid=10,@toid=20
select c.* from authors c,
(select a.au_id,count(a.au_id) as id_no from authors a,authors b where a.au_id>b.au_id group by a.au_id) d
where c.au_id=d.au_id and id_no between @fromid and @toid
方法二:
先把数据存到一个#table1中然后生成一ID_NO,然后依ID_NO取想要的数据
例:
use pubs
declare @fromid int
declare @toid int
select @fromid=10,@toid=20
if object_id('tempdb..#table1') is not null
drop table tempdb..#table1
select IDENTITY(int, 1,1) AS ID_NO,* into #table1 from authors
select * from #table1 where id_no between @fromid and @toid
SqlConnection objConnection;
string Select;
int intStartIndex;
int intEndIndex;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
SqlConnection cmdSelect;
objConnection=new SqlConnection(ConfigurationSettings.AppSettings["SQLConnect"]);
if(!IsPostBack)
{
Select=@"SELECT Count(*) FROM ControlFlowCompInfo";
SqlCommand cmd = new SqlCommand(Select,objConnection);
objConnection.Open(); DataGrid1.VirtualItemCount=5;//(cmd.ExecuteScalar()/ DataGrid1.PageSize); objConnection.Close();
BindDataGrid();
}
}private void BindDataGrid()
{
SqlDataAdapter dadProducts;
DataSet dstProducts;
intEndIndex = intStartIndex + DataGrid1.PageSize;
Select = @"SELECT ID,Major,Minor,Name,Note,Description,KnowledgeInfo FROM ControlFlowCompInfo Where ID>@startIndex " + @"And ID<=@EndIndex Order By ID"; dadProducts = new SqlDataAdapter(Select,objConnection);
dadProducts.SelectCommand.Parameters.Add("@startIndex",intStartIndex);
dadProducts.SelectCommand.Parameters.Add("@endIndex",intEndIndex);
dstProducts=new DataSet();
dadProducts.Fill(dstProducts); DataGrid1.DataSource = dstProducts;
DataGrid1.DataBind();
}private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
intStartIndex=(e.NewPageIndex * DataGrid1.PageSize);
DataGrid1.CurrentPageIndex = e.NewPageIndex;
BindDataGrid();
}