http://xml.sz.luohuedu.net/xml/ShowDetail.asp?id=B12283DE-DB20-4322-ACCC-12724442808A
解决方案 »
- visual SourceSafe配置问题
- 如何获取TreeView里的值,不是ID
- asp.net与exchang server的开发 接口问题
- 为什么修改selectedindex的值,页面还是显示第一个
- 看了几个月了,发表一下自己最近的一些想法 大伙来看看
- System.Xml.XmlException: 缺少根元素
- 有介绍CSS各个属性的电子书吗
- DropDownList1_SelectedIndexChanged()為何備執行兩次
- 外包经验的人请进
- BinaryFormatter 版本不兼容,为什么?
- 水晶报表的问题,急!急急急急急
- 急需求教:怎么样在asp.net里面调出word文档?
对于表中主键或标识列的情况,直接从原表取数查询,其它情况使用临时表的方法
如果视图或查询结果中有主键,不推荐此方法--邹建 2003.09--*//*--调用示例
exec p_show '地区资料' exec p_show '地区资料',5,3,'地区编号,地区名称,助记码','地区编号'
--*/
/*--调用示例 *********************************注意加top 100 percent******************
use haihuioa
exec p_show 'select top 100 percent * from clientservicelog where fkid=1 and pdate>=''2003-12-1'' and pdate<=''2003-12-3'' ',5,1,'','pdate asc'
go
--*/
/*
因为要顾及通用性,所以对带排序的查询语句有一定要求.如果先排序,再出结果.就是:exec p_show 'select top 100 percent * from 地区资料 order by 地区名称',5,3,'地区编号,地区名称,助记码'
--查询语句加上:top 100 percent
*/
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[p_show]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[p_show]
GOCREATE Proc p_show
@QueryStr nvarchar(4000), --表名、视图名、查询语句
@PageSize int=5, --每页的大小(行数)
@PageCurrent int=1, --要显示的页
@FdShow nvarchar (4000)='', --要显示的字段列表,如果查询结果有标识字段,需要指定此值,且不包含标识字段
@FdOrder nvarchar (1000)='' --排序字段列表
as
declare @FdName nvarchar(250) --表中的主键或表、临时表中的标识列名
,@Id1 varchar(20),@Id2 varchar(20) --开始和结束的记录号
,@Obj_ID int --对象ID
--表中有复合主键的处理
declare @strfd nvarchar(2000) --复合主键列表
,@strjoin nvarchar(4000) --连接字段
,@strwhere nvarchar(2000) --查询条件
select @Obj_ID=object_id(@QueryStr)
,@FdShow=case isnull(@FdShow,'') when '' then ' *' else ' '+@FdShow end
,@FdOrder=case isnull(@FdOrder,'') when '' then '' else ' order by '+@FdOrder end
,@QueryStr=case when @Obj_ID is not null then ' '+@QueryStr else ' ('+@QueryStr+') a' end--如果显示第一页,可以直接用top来完成
if @PageCurrent=1
begin
select @Id1=cast(@PageSize as varchar(20))
exec('select top '+@Id1+@FdShow+' from '+@QueryStr+@FdOrder)
return
end--如果是表,则检查表中是否有标识更或主键
if @Obj_ID is not null and objectproperty(@Obj_ID,'IsTable')=1
begin
select @Id1=cast(@PageSize as varchar(20))
,@Id2=cast((@PageCurrent-1)*@PageSize as varchar(20)) select @FdName=name from syscolumns where id=@Obj_ID and status=0x80
if @@rowcount=0 --如果表中无标识列,则检查表中是否有主键
begin
if not exists(select 1 from sysobjects where parent_obj=@Obj_ID and xtype='PK')
goto lbusetemp --如果表中无主键,则用临时表处理 select @FdName=name from syscolumns where id=@Obj_ID and colid in(
select colid from sysindexkeys where @Obj_ID=id and indid in(
select indid from sysindexes where @Obj_ID=id and name in(
select name from sysobjects where xtype='PK' and parent_obj=@Obj_ID
)))
if @@rowcount>1 --检查表中的主键是否为复合主键
begin
select @strfd='',@strjoin='',@strwhere=''
select @strfd=@strfd+',['+name+']'
,@strjoin=@strjoin+' and a.['+name+']=b.['+name+']'
,@strwhere=@strwhere+' and b.['+name+'] is null'
from syscolumns where id=@Obj_ID and colid in(
select colid from sysindexkeys where @Obj_ID=id and indid in(
select indid from sysindexes where @Obj_ID=id and name in(
select name from sysobjects where xtype='PK' and parent_obj=@Obj_ID
)))
select @strfd=substring(@strfd,2,2000)
,@strjoin=substring(@strjoin,5,4000)
,@strwhere=substring(@strwhere,5,4000)
goto lbusepk
end
end
end
else
goto lbusetemp/*--使用标识列或主键为单一字段的处理方法--*/
lbuseidentity:
exec('select top '+@Id1+@FdShow+' from '+@QueryStr
+' where '+@FdName+' not in(select top '
+@Id2+' '+@FdName+' from '+@QueryStr+@FdOrder
+')'+@FdOrder
)
return/*--表中有复合主键的处理方法--*/
lbusepk:
exec('select '+@FdShow+' from(select top '+@Id1+' a.* from
(select top 100 percent * from '+@QueryStr+@FdOrder+') a
left join (select top '+@Id2+' '+@strfd+'
from '+@QueryStr+@FdOrder+') b on '+@strjoin+'
where '+@strwhere+') a'
)
return/*--用临时表处理的方法--*/
lbusetemp:
select @FdName='[ID_'+cast(newid() as varchar(40))+']'
,@Id1=cast(@PageSize*(@PageCurrent-1) as varchar(20))
,@Id2=cast(@PageSize*@PageCurrent-1 as varchar(20))exec('select '+@FdName+'=identity(int,0,1),'+@FdShow+'
into #tb from'+@QueryStr+@FdOrder+'
select '+@FdShow+' from #tb where '+@FdName+' between '
+@Id1+' and '+@Id2
)GO
ViewState保存 SQL语句 起始页面 排序字段 正序asc还是desc这样就可以很好的调用了
这样做是为了 排序 和分页 及查询的 需要
如 你没这么高的要求 可以自己手工写
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Configuration;
namespace Agora
{
/// <summary>
/// 对页面元素进行样式设置
/// </summary>
public class _DataGridSet
{
/// <summary>
/// 对DataGrid进行样式设置
/// </summary>
/// <param name="ThisElement">DataGrid的名字</param>
public void _DataGridStyle(DataGrid ThisElement)
{
getDataGridStyle(ThisElement, 0);
}
/// <summary>
/// 对DataGrid进行样式设置
/// </summary>
/// <param name="ThisElement">DataGrid的名字</param>
/// <param name="Pages">每页的条数</param>
public void _DataGridStyle(DataGrid ThisElement, int Pages)
{
getDataGridStyle(ThisElement, Pages);
}
private void getDataGridStyle(DataGrid ThisElement, int ThisPages)
{ string DataGridBorderColor = "#bdbabd"; //DataGrid 边框的颜色
int DataGridBorderWidth = 1;
int DataGridCellPadding = 5; //DataGrid 行间距
int DataGridFontSize = 9; //DataGrid 文字的大小
string DataGridwidth = "100%"; //DataGrid 宽度 string DataGridHeaderBackgroundImg = "../Images/007.gif"; //DataGrid 标题栏背景图片
int DataGridHeaderHeight = 25; //DataGrid 标题栏高度
HorizontalAlign DataGridHeaderAlign = HorizontalAlign.Center; //DataGrid 标题栏文字的位置(居中,靠左,靠右)
string DataGridHeaderFontColor = "#000000"; //DataGrid 标题栏文字颜色 string DataGridItemBackColor = "#FFFFFF"; //DataGrid 列表栏背景色
int DataGridItemHeight = 25; //DataGrid 列表栏的高度
string DataGridItemFontColor = "#000000"; //DataGrid 列表栏的文字颜色 string DataGridItemSwepBackColor = "#f7f3ef"; //DataGrid 列表栏过渡栏的背景色
int DataGridItemSwepHeight = 25; //DataGrid 列表栏过渡栏的高度
string DataGridItemSwepFontColor = "#000000"; //DataGrid 列表栏过渡栏的文字颜色
ThisElement.BorderColor = Color.FromName(DataGridBorderColor);
ThisElement.BorderWidth = DataGridBorderWidth;
ThisElement.CellPadding = DataGridCellPadding;
ThisElement.Font.Size = DataGridFontSize;
ThisElement.Attributes["width"] = DataGridwidth; ThisElement.Attributes["style"] = "background-image: url('"+ DataGridHeaderBackgroundImg +"')";
ThisElement.HeaderStyle.Height = DataGridHeaderHeight;
ThisElement.HeaderStyle.HorizontalAlign = DataGridHeaderAlign;
ThisElement.HeaderStyle.ForeColor = Color.FromName(DataGridHeaderFontColor); ThisElement.ItemStyle.BackColor = Color.FromName(DataGridItemBackColor);
ThisElement.ItemStyle.Height = DataGridItemHeight;
ThisElement.ItemStyle.ForeColor = Color.FromName(DataGridItemFontColor); ThisElement.AlternatingItemStyle.BackColor = Color.FromName(DataGridItemSwepBackColor);
ThisElement.AlternatingItemStyle.Height = DataGridItemSwepHeight;
ThisElement.AlternatingItemStyle.ForeColor = Color.FromName(DataGridItemSwepFontColor); ThisElement.DataBind();
if(ThisPages != 0)
{ ThisElement.PageSize = ThisPages;
ThisElement.AllowPaging = true;
ThisElement.Height = 35;
ThisElement.PagerStyle.VerticalAlign = VerticalAlign.Bottom;
ThisElement.PagerStyle.HorizontalAlign = HorizontalAlign.Right;
ThisElement.PagerStyle.BackColor = Color.FromName("#F7F3F3");
ThisElement.DataBind(); //获取当前递交的页数
int ThisDataGridPage = 1;
if(HttpContext.Current.Request.Form["DataGridPageSize"] != null)
{
try
{
ThisDataGridPage = Convert.ToInt32(HttpContext.Current.Request.Form["DataGridPageSize"].ToString().Trim());
if(ThisDataGridPage < 1)
{
ThisDataGridPage = 1;
}
if(ThisDataGridPage > ThisElement.PageCount)
{
ThisDataGridPage = ThisElement.PageCount;
}
}
catch(Exception)
{
ThisDataGridPage = 1;
}
}
ThisElement.CurrentPageIndex = ThisDataGridPage;
//页面信息
string strThisPageName = "<span style='color:#ff0000'>"+ ThisDataGridPage.ToString() +"</span>";
string strApageCount = "<span style='color:#ff0000'>"+ ThisElement.PageSize.ToString() +"</span>";
string strAllPage = "<span style='color:#ff0000'>"+ ThisElement.PageCount.ToString() +"</span>";
string strPageInfo = "当前是 "+ strThisPageName +"/"+ strAllPage +" 每页显示 "+ strApageCount +" 条";
//第一页
string strPage1 = "<span onMouseover=this.style.color='#ff0000' onMouseout=this.style.color='#000000' onclick='document.all.DataGridPageSize.value = 1;document.all.tags(\"form\")[0].submit();' style='cursor:hand'>第一页</span>";
if(ThisDataGridPage == 1)
{
strPage1 = "<span disabled>第一页</span>";
} //上一页
string strPage2 = "<span onMouseover=this.style.color='#ff0000' onMouseout=this.style.color='#000000' onclick='document.all.DataGridPageSize.value = "+ Convert.ToString(ThisDataGridPage-1) +";document.all.tags(\"form\")[0].submit();' style='cursor:hand'>上一页</span>";
if(ThisDataGridPage == 1)
{
strPage2 = "<span disabled>上一页</span>";
} //下一页
string strPage3 = "<span onMouseover=this.style.color='#ff0000' onMouseout=this.style.color='#000000' onclick='document.all.DataGridPageSize.value = "+ Convert.ToString(ThisDataGridPage+1) +";document.all.tags(\"form\")[0].submit();' style='cursor:hand'>下一页</span>";
if(ThisDataGridPage == ThisElement.PageCount)
{
strPage3 = "<span disabled>下一页</span>";
} //最后一页
string strPage4 = "<span onMouseover=this.style.color='#ff0000' onMouseout=this.style.color='#000000' onclick='document.all.DataGridPageSize.value = "+ ThisElement.PageCount.ToString() +";document.all.tags(\"form\")[0].submit();' style='cursor:hand'>最后一页</span>";
if(ThisDataGridPage == ThisElement.PageCount)
{
strPage4 = "<span disabled>最后一页</span>";
} //跳到
string strGotoPage = "跳转到 <input type='text' name='DataGridPageSize' style='width:20px'>";
StringBuilder PageText = new StringBuilder();
PageText.Append("</a><span onselectstart='return false'>"+ strPageInfo +" "+ strPage1 +" "+ strPage2 +" "+ strPage3 +" "+ strPage4 +" "+ strGotoPage +"</span><a>"); ThisElement.PagerStyle.NextPageText = PageText.ToString();
ThisElement.CurrentPageIndex = ThisDataGridPage - 1;
//添加分页设置
ThisElement.PagerStyle.NextPageText = PageText.ToString();
ThisElement.PagerStyle.PrevPageText = "";
}
}
}
}