思路就是 查一部分数据 显示一部分数据比如先查100条数据出来显示,然后点下一步的时候又查询100条数据(前面100不查)我这里有个分页的存储过程 Create PROCEDURE [dbo].[sp_desc_GetDataSet] @PageSize int, @strWhat varchar(50), @PageCount int, @strWhere varchar(500), @sptable varchar(50) AS declare @sql varchar(5000) declare @sqll varchar(5000) set @sql=' select Top '+cast(@PageSize AS varchar)+' * from '+@sptable+' where ('+@strWhat+' < (select min('+@strWhat+') from (select top '+cast(@PageCount AS varchar)+' '+ @strWhat+' from '+@sptable+' where '+@strWhere+' order by '+@strWhat+' desc) as T )) and '+@strWhere+' order by '+@strWhat+' desc' set @sqll = ' select Top '+cast(@PageSize AS varchar)+' * from '+@sptable+' where '+@strWhere+' order by '+@strWhat+' desc' if ((cast(@PageCount AS varchar)) != 0) exec(@sql) else exec(@sqll)
可以建立一个视图,然后来查询分页这个视图的数据,存储过程如下: 没有主键也行,但是你要为下面的存储过程指定一个你认为可以作为主键的字段。 下面的@talName参数,就传你的试图名称。------------------------------------ --用途:分页存储过程(对有主键的表效率极高) --说明: ------------------------------------ CREATE PROCEDURE [dbo].[UP_GetRecordByPage] @tblName varchar(255), -- 表名 @fldName varchar(255), -- 主键字段名 @PageSize int = 10, -- 页尺寸 @PageIndex int = 1, -- 页码 @IsReCount bit = 0, -- 返回记录总数, 非 0 值则返回 @OrderType bit = 0, -- 设置排序类型, 非 0 值则降序 @strWhere varchar(1000) = '' -- 查询条件 (注意: 不要加 where) AS declare @strSQL varchar(6000) -- 主语句 declare @strTmp varchar(100) -- 临时变量(查询条件过长时可能会出错,可修改100为1000) declare @strOrder varchar(400) -- 排序类型 if @OrderType != 0 begin set @strTmp = '<(select min' set @strOrder = ' order by [' + @fldName +'] desc' end else begin set @strTmp = '>(select max' set @strOrder = ' order by [' + @fldName +'] asc' end set @strSQL = 'select top ' + str(@PageSize) + ' * from [' + @tblName + '] where [' + @fldName + ']' + @strTmp + '([' + @fldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' [' + @fldName + '] from [' + @tblName + ']' + @strOrder + ') as tblTmp)' + @strOrder if @strWhere != '' set @strSQL = 'select top ' + str(@PageSize) + ' * from [' + @tblName + '] where [' + @fldName + ']' + @strTmp + '([' + @fldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' [' + @fldName + '] from [' + @tblName + '] where ' + @strWhere + ' ' + @strOrder + ') as tblTmp) and ' + @strWhere + ' ' + @strOrder if @PageIndex = 1 begin set @strTmp ='' if @strWhere != '' set @strTmp = ' where ' + @strWhere set @strSQL = 'select top ' + str(@PageSize) + ' * from [' + @tblName + ']' + @strTmp + ' ' + @strOrder end if @IsReCount != 0 set @strSQL = 'select count(*) as Total from [' + @tblName + ']'+' where ' + @strWhere exec (@strSQL)
ALTER PROCEDURE proc @pageSize int, @pageIndex int,@orderField varchar(20) ='PublishDate',AS declare @begin int ,@end int set @begin=@pageSize*(@pageIndex-1)+1 set @end=@pageSize*@pageIndex exec('select * from (select row_number() over (order by '+@orderField +') as row_id,* from 视图) as temp where row_id between '+ @begin+' and '+@end+'') RETURN
参考
如果是vs2005版本 直接 有个设置 页面哈
drop proc proc_getByPage
go
create proc proc_getByPage
@startIndex int,@count int,@order nvarchar(20)
as
declare @sql nvarchar(1000)
set @sql=
'select top '+CONVERT(nvarchar(5),@count)+' * from Student
where ID not in
(
select top '+CONVERT(nvarchar(5),@startIndex)+' ID from Student order by '+@order+'
)
order by '+@order
exec (@sql)
goexec proc_getByPage 0,10,'ID'C#代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;using System.Data.SqlClient;namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//要显示的行数
int pageSize = 10;
//总的页数
int pageCount = 0;
//当前第几页
int currentPage = 0;
SqlConnection conn = new SqlConnection(@"Data Source=4DE67B3C5AF94F7\SQLEXPRESS;Initial Catalog=TTTTT;Integrated Security=True"); private void Form1_Load(object sender, EventArgs e)
{ SetPageCount();
UpdateUI();
BindGrid();
}
/// <summary>
/// 求到总的条数
/// </summary>
/// <returns></returns>
public int Count()
{
int sum = 0;
string sql = "select count(*) from Student";
conn.Open();
SqlCommand con = new SqlCommand(sql,conn);
sum = (int)con.ExecuteScalar(); return sum;
}
//算出总页数
private void SetPageCount()
{
try
{
int recordCount = Count();
pageCount = (pageSize + recordCount - 1) / pageSize;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
//更新界面
private void UpdateUI()
{
this.label1.Text = string.Format("共{0}页,当前第{1}页", pageCount, currentPage + 1);
this.textBox1.Text = (currentPage + 1).ToString();
if (currentPage <= 0)
this.button1.Enabled = false;
else
this.button1.Enabled = true;
if (currentPage >= pageCount - 1)
this.button2.Enabled = false;
else
this.button2.Enabled = true;
}
//绑定数据
private void BindGrid()
{
try
{
string sql = string.Format("exec proc_getByPage {0},{1},'ID'", pageSize * currentPage, pageSize);
SqlDataAdapter data = new SqlDataAdapter(sql, conn);
DataSet ds = new DataSet(); data.Fill(ds, "Student");
this.dataGridView1.DataSource = ds.Tables["Student"];
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
} /// <summary>
/// 下一页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
if (currentPage < pageCount - 1)
{
currentPage++;
BindGrid();
UpdateUI();
} }
/// <summary>
/// 上一页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
if (currentPage > 0)
{
currentPage--;
BindGrid();
UpdateUI();
} }
/// <summary>
/// 跳转
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button3_Click(object sender, EventArgs e)
{
int page = 0;
if (int.TryParse(this.textBox1.Text, out page))
{
if (page < 1)
page = 1;
if (page > pageCount)
page = pageCount;
currentPage = page - 1;
BindGrid();
UpdateUI();
}
else
{
MessageBox.Show("请输入数字", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
textBox1.Clear();
textBox1.Focus();
} }
/// <summary>
/// 首页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button4_Click(object sender, EventArgs e)
{
currentPage = 0;
BindGrid();
UpdateUI();
}
/// <summary>
/// 最后一页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button5_Click(object sender, EventArgs e)
{
currentPage = pageCount - 1;
BindGrid();
UpdateUI();
} }
}
create proc up_SelCdtData
(
@SceneID int
)
as
begin
select * from (
(select B.[Name],B.InformationTypeName,
B.InformationName,A.[Id],A.Value,A.IsValid,
A.SourceSite,A.DestSite,A.ComDateTime
from
(select *
from CdtData
where (FnCode>=240 AND FnCode<=255)) as A,
(select Account.[ID],Account.[Name],
Information.InformationId,
InformationType.InformationTypeName,
Information.InformationValue,
Information.InformationName
from Account,Information,InformationType
where Information.InformationTypeId=InformationType.InformationTypeId and
Account.[ID]=Information.AccountId and
Information.InformationTypeId=1 and
Account.SceneID=@SceneID) as B
where A.[Id]=B.InformationId)
union
(select B.[Name],B.InformationTypeName,
B.InformationName,A.[Id],A.Value,A.IsValid,
A.SourceSite,A.DestSite,A.ComDateTime
from
(select *
from CdtData
where (FnCode>=0 AND FnCode<=127)) as A,
(select Account.[ID],Account.[Name],
Information.InformationId,
InformationType.InformationTypeName,
Information.InformationValue,
Information.InformationName
from Account,Information,InformationType
where Information.InformationTypeId=InformationType.InformationTypeId and
Account.[ID]=Information.AccountId and
Information.InformationTypeId=2 and
Account.SceneID=@SceneID) as B
where A.[Id]=B.InformationId)
)as C
order by C.ComDateTime desc
end得到的结果是:
很多条数据
电视1 1信号 温度 749 8 81 1 0 0 32:37.0
电视2 2信号 压强 748 7 71 1 0 0 32:37.0
电视3 3信号 温度 747 6 61 1 0 0 32:37.0
电视1 1信号 温度 749 8 81 1 0 0 32:37.0
电视2 2信号 压强 748 7 71 1 0 0 32:37.0
电视3 3信号 温度 747 6 61 1 0 0 32:37.0
电视1 1信号 温度 749 8 81 1 0 0 32:37.0
电视2 2信号 压强 748 7 71 1 0 0 32:37.0
电视3 3信号 温度 747 6 61 1 0 0 32:37.0
.
.
.
.
.
.
这个怎么分页哦???
将allowpaging设置为true
你在它的SelectedIndexChanging事件里面这些写就可以了:
protected void GridView1_SelectedIndexChanging(object sender, EventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
bindData();
}当然,你也可以在存储过程里面分页,可能会麻烦点
2.默认1页10条,如果要修改每页条数,修改PageSize即可,在aspx代码中是PageSize="12"。
3.默认的是单向排序的,右击GridView弹出“属性”,选择AllowSorting为True即可。
一样的。winform里面的datagridview是没有分页属性的。还有没有其他的方法哦??
Create PROCEDURE [dbo].[sp_desc_GetDataSet]
@PageSize int,
@strWhat varchar(50),
@PageCount int,
@strWhere varchar(500),
@sptable varchar(50)
AS
declare @sql varchar(5000)
declare @sqll varchar(5000)
set @sql=' select Top '+cast(@PageSize AS varchar)+' * from '+@sptable+' where ('+@strWhat+' < (select min('+@strWhat+') from (select top '+cast(@PageCount AS varchar)+' '+ @strWhat+' from '+@sptable+' where '+@strWhere+' order by '+@strWhat+' desc) as T )) and '+@strWhere+' order by '+@strWhat+' desc'
set @sqll = ' select Top '+cast(@PageSize AS varchar)+' * from '+@sptable+' where '+@strWhere+' order by '+@strWhat+' desc'
if ((cast(@PageCount AS varchar)) != 0)
exec(@sql)
else
exec(@sqll)
没有主键也行,但是你要为下面的存储过程指定一个你认为可以作为主键的字段。
下面的@talName参数,就传你的试图名称。------------------------------------
--用途:分页存储过程(对有主键的表效率极高)
--说明:
------------------------------------
CREATE PROCEDURE [dbo].[UP_GetRecordByPage]
@tblName varchar(255), -- 表名
@fldName varchar(255), -- 主键字段名
@PageSize int = 10, -- 页尺寸
@PageIndex int = 1, -- 页码
@IsReCount bit = 0, -- 返回记录总数, 非 0 值则返回
@OrderType bit = 0, -- 设置排序类型, 非 0 值则降序
@strWhere varchar(1000) = '' -- 查询条件 (注意: 不要加 where)
AS
declare @strSQL varchar(6000) -- 主语句
declare @strTmp varchar(100) -- 临时变量(查询条件过长时可能会出错,可修改100为1000)
declare @strOrder varchar(400) -- 排序类型
if @OrderType != 0
begin
set @strTmp = '<(select min'
set @strOrder = ' order by [' + @fldName +'] desc'
end
else
begin
set @strTmp = '>(select max'
set @strOrder = ' order by [' + @fldName +'] asc'
end
set @strSQL = 'select top ' + str(@PageSize) + ' * from ['
+ @tblName + '] where [' + @fldName + ']' + @strTmp + '(['
+ @fldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' ['
+ @fldName + '] from [' + @tblName + ']' + @strOrder + ') as tblTmp)'
+ @strOrder
if @strWhere != ''
set @strSQL = 'select top ' + str(@PageSize) + ' * from ['
+ @tblName + '] where [' + @fldName + ']' + @strTmp + '(['
+ @fldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' ['
+ @fldName + '] from [' + @tblName + '] where ' + @strWhere + ' '
+ @strOrder + ') as tblTmp) and ' + @strWhere + ' ' + @strOrder
if @PageIndex = 1
begin
set @strTmp =''
if @strWhere != ''
set @strTmp = ' where ' + @strWhere
set @strSQL = 'select top ' + str(@PageSize) + ' * from ['
+ @tblName + ']' + @strTmp + ' ' + @strOrder
end
if @IsReCount != 0
set @strSQL = 'select count(*) as Total from [' + @tblName + ']'+' where ' + @strWhere
exec (@strSQL)
电视1 1信号 温度 749 8 81 1 0 0 32:37.0
电视2 2信号 压强 748 7 71 1 0 0 32:37.0
电视3 3信号 温度 747 6 61 1 0 0 32:37.0
电视1 1信号 温度 749 8 81 1 0 0 32:37.0
电视2 2信号 压强 748 7 71 1 0 0 32:37.0
电视3 3信号 温度 747 6 61 1 0 0 32:37.0
电视1 1信号 温度 749 8 81 1 0 0 32:37.0
电视2 2信号 压强 748 7 71 1 0 0 32:37.0
电视3 3信号 温度 747 6 61 1 0 0 32:37.0
.
.
.
.
.
.我要怎么将这些数据快速的插入到临时表里面呢???
请大家帮我看看。
ALTER PROCEDURE proc
@pageSize int,
@pageIndex int,@orderField varchar(20) ='PublishDate',AS
declare @begin int ,@end int
set @begin=@pageSize*(@pageIndex-1)+1
set @end=@pageSize*@pageIndex
exec('select * from (select row_number() over (order by '+@orderField +')
as row_id,* from 视图) as temp
where row_id between '+ @begin+' and '+@end+'')
RETURN