|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
也许C#刚上市的时候有些抄袭Java吧,但自从C#2.0上市之后,整个局面就扭转乾坤了,不但Java在模仿C#,而且他从来都没能跟得上C#的脚步。asp.net|分页|技能分离一个存储历程,将分页做成最复杂,请看以下源码
此分页类所操纵的存储历程#region此分页类所操纵的存储历程
/**//*********************************************************
*
*功效壮大,共同以下这个存储历程
*
********************************************************/
/**//*
--Pager1,10,0,0,EmployeeID>2andEmployeeID<5,Employees,*,LastName,0
CREATEPROCEDUREPager
@PageIndexint,--索引页1
@PageSizeint,--每页数目2
@RecordCountintout,--总行数3
@PageCountintout,--总页数4
@WhereConditionNvarchar(1000),--查询前提5
@TableNamenvarchar(500),--查询表名6
@SelectStrnvarchar(500)=*,--查询的列7
@Ordernvarchar(500),--排序的列8
@OrderTypebit=0,--设置排序范例,非0值则降序9
@GroupbyNVarChar(100)=
AS
declare@strSQLnvarchar(2000)--主语句
declare@strTmpnvarchar(1000)--一时变量
declare@strOrdernvarchar(1000)--排序范例
if@OrderType!=0
begin
set@strTmp=<(selectmin
set@strOrder=orderby+@Order+desc
end
else
begin
set@strTmp=>(selectmax
set@strOrder=orderby+@Order+asc
end
set@strSQL=selecttop+str(@PageSize)++@SelectStr+from
+@TableName+where+@Order++@strTmp+([
+@Order+])from(selecttop+str((@PageIndex-1)*@PageSize)+[
+@Order+]from+@TableName++@strOrder+)astblTmp)
+@Groupby+@strOrder
if@WhereCondition!=
set@strSQL=selecttop+str(@PageSize)++@SelectStr+from
+@TableName+where+@Order++@strTmp+([
+@Order+])from(selecttop+str((@PageIndex-1)*@PageSize)+[
+@Order+]from+@TableName+where(+@WhereCondition+)
+@strOrder+)astblTmp)and(+@WhereCondition+)+@Groupby+@strOrder
if@PageIndex=1
begin
set@strTmp=
if@WhereCondition!=
set@strTmp=where(+@WhereCondition+)
set@strSQL=selecttop+str(@PageSize)++@SelectStr+from
+@TableName++@strTmp++@Groupby+@strOrder
end
exec(@strSQL)
--print@strSQL
IF@WhereCondition
Begin
SET@strTmp=SELECT-1FROM+@TableName+Where+(@WhereCondition)
End
ELSE
Begin
SET@strTmp=SELECT-1FROM+@TableName
End
EXECSP_EXECUTESQL@strTmp
SET@RecordCount=@@RowCount
--猎取总页数
--"CEILING"函数:获得不小于某数的最小整数
SET@PageCount=CEILING(@RecordCount*1.0/@PageSize)
GO
*****************************************************************************/
/**//****************************************************************************
*
*用法
*
****************************************************************************/
/**//*
DimtsAsString=Request.Form.Item("txtDate")
If(ts=""OrtsIsNothing)Then
ts=Request.QueryString("txtDate")
EndIf
Dimts2AsString=Request.Form.Item("txtDate2")
If(ts2=""Orts2IsNothing)Then
ts2=Request.QueryString("txtDate2")
EndIf
DimpsAsString=Request.Form.Item("pageIndex")
If(ps=""OrpsIsNothing)Then
ps=Request.QueryString("pageIndex")
EndIf
DimtAsInteger=2
DimpAsInteger=1
IftsIsNothingThen
ts=""
EndIf
IfpsIsNothingThen
ps=""
EndIf
IfNot(ps="")Then
p=Integer.Parse(ps)
EndIf
DimpagerAsPager=NewPager
pager.PageIndex=p
pager.PageSize=20
pager.PageMode=PageMode.Str
pager.WhereCondition="TheDatebetweenconvert(datetime,"+ts+")andconvert(datetime,"+ts2+")"
pager.WhereCondition="convert(char(10),TheDate,120)="+ts+""
pager.TableName="LoadCountlog"
pager.SelectStr="*"
pager.Order="ID"
pager.OrderType=False
DimdtAsSystem.Data.DataTable=pager.GetDatas(p)
myDataGrid.DataSource=dt
myDataGrid.DataBind()
DimgoUrlAsString="WebForm1.aspx?txtDate="+ts+"&txtDate2="+ts2
Me.Label3.Text="共:"+pager.PageCount.ToString+"页,"+pager.RecordCount.ToString()+"条<strong>"+pager.OutPager(pager,goUrl,False)+"</strong>"
*/
#endregion
usingSystem;
usingSystem.Data;
usingSystem.Data.SqlClient;
usingSystem.Configuration;
usingSystem.Collections;
usingSystem.Text;
namespacesolucky
{
/**////<summary>
///分页形式
///</summary>
publicenumPageMode
{
/**////<summary>
///数字分页
///</summary>
Num=0,
/**////<summary>
///字符分页
///</summary>
Str=1
}
/**////<summary>
///分页类,能过存储历程举行分页,功效相称壮大。
///</summary>
publicclassPager
{
privateintpageIndex=0;
privateintrecordCount=0;
privateintpageSize=20;
privateintpageCount=0;
privateintrowCount=0;
privatestringtableName="";
privatestringwhereCondition="1=1";
privatestringselectStr="*";
privatestringorder="";
privatestringprocedure="pager";
privateboolorderType=true;
privatePageModepageMode=PageMode.Num;
privatestringsqlConnectionString=ConfigurationSettings.AppSettings["database"];
privatestringdatabaseOwner="dbo";
数据毗连#region数据毗连
/**////<summary>
///数据毗连字符串
///</summary>
privatestringSqlConnectionString
{
get
{
returnthis.sqlConnectionString;
}
set
{
this.sqlConnectionString=value;
}
}
/**////<summary>
///猎取毗连实例
///</summary>
///<returns></returns>
privateSqlConnectionGetSqlConnectionString()
{
try
{
returnnewSqlConnection(SqlConnectionString);
}
catch
{
thrownewException("SQLConnectionStringisinvalid.");
}
}
/**////<summary>
///数据工具一切者
///</summary>
privatestringDatabaseOwner
{
get
{
returnthis.databaseOwner;
}
set{
this.databaseOwner=value;
}
}
#endregion
publicPager()
{
//
//TODO:在此处增加机关函数逻辑
//
//Enum.Parse(tyo
}
publicPager(stringconnstr)
{
if(connstr!=null)
this.SqlConnectionString=connstr;
}
#region
/**////<summary>
///所要操纵的存储历程称号,已有默许的分页存储历程
///</summary>
publicstringProcedure
{
get{
returnthis.procedure;
}
set{
if(value==null||value.Length<=0)
{
this.procedure="pager";
}
else
{
this.procedure=value;
}
}
}
/**////<summary>
///以后所要显现的页面数
///</summary>
publicintPageIndex
{
get
{
returnthis.pageIndex;
}
set
{
this.pageIndex=value;
}
}
/**////<summary>
///总的页面数
///</summary>
publicintPageCount
{
get
{
returnthis.pageCount;
}
set
{
this.pageCount=value;
}
}
/**////<summary>
///总行数
///</summary>
publicintRecordCount
{
get
{
returnthis.recordCount;
}
set
{
this.recordCount=value;
}
}
/**////<summary>
///每页条数
///</summary>
publicintPageSize
{
get
{
returnthis.pageSize;
}
set
{
this.pageSize=value;
}
}
/**////<summary>
///表称号
///</summary>
publicstringTableName
{
get
{
returntableName;
}
set
{
this.tableName=value;
}
}
/**////<summary>
///前提查询
///</summary>
publicstringWhereCondition
{
get
{
returnwhereCondition;
}
set
{
whereCondition=value;
}
}
/**////<summary>
///查询方针(搜刮方针),好比:AddTimeAS工夫,IDAS编号
///</summary>
publicstringSelectStr
{
get
{
returnselectStr;
}
set
{
selectStr=value;
}
}
/**////<summary>
///排序的列
///</summary>
publicstringOrder
{
get
{
returnorder;
}
set
{
order=value;
}
}
/**////<summary>
///排序范例true:ascfalse:desc
///</summary>
publicboolOrderType
{
get
{
returnorderType;
}
set
{
orderType=value;
}
}
/**////<summary>
///分页形式
///</summary>
publicPageModePageMode
{
get
{
returnthis.pageMode;
}
set
{
this.pageMode=value;
}
}
/**////<summary>
///失掉以后前往的数目
///</summary>
publicintRowCount
{
get
{
returnthis.rowCount;
}
}
privatestringgroupby;
publicstringGroupby
{
get
{
returnthis.groupby;
}
set
{
this.groupby=value;
}
}
#endregion
/**////<summary>
///分页查寻了局
///</summary>
publicDataTableGetDatas(intpageIndex)
{
this.pageIndex=pageIndex;
Pagerpager=this;
//pager.pageIndex=pageIndex;
DataTablereturnTb=Pagination(refpager).Tables[0];
this.rowCount=returnTb.Rows.Count;
returnreturnTb;
}
/**////<summary>
///分页操纵存储历程函数
///</summary>
///<paramname="pager"></param>
///<returns></returns>
privateDataSetPagination(refPagerpager)
{
using(SqlConnectionmyConnection=GetSqlConnectionString())
{
SqlDataAdaptermyCommand=newSqlDataAdapter(pager.databaseOwner+"."+pager.Procedure,myConnection);
myCommand.SelectCommand.CommandType=CommandType.StoredProcedure;
SqlParameterparameterPageIndex=newSqlParameter("@PageIndex",SqlDbType.Int);
parameterPageIndex.Value=pager.PageIndex;
myCommand.SelectCommand.Parameters.Add(parameterPageIndex);
SqlParameterparameterPageSize=newSqlParameter("@PageSize",SqlDbType.Int);
parameterPageSize.Value=pager.PageSize;
myCommand.SelectCommand.Parameters.Add(parameterPageSize);
SqlParameterparameterRecordCount=newSqlParameter("@RecordCount",SqlDbType.Int);
parameterRecordCount.Value=0;
parameterRecordCount.Direction=ParameterDirection.InputOutput;
myCommand.SelectCommand.Parameters.Add(parameterRecordCount);
SqlParameterparameterPageCount=newSqlParameter("@PageCount",SqlDbType.Int);
parameterPageCount.Value=0;
parameterPageCount.Direction=ParameterDirection.InputOutput;
myCommand.SelectCommand.Parameters.Add(parameterPageCount);
SqlParameterparameterWhereCondition=newSqlParameter("@WhereCondition",SqlDbType.NVarChar,500);
parameterWhereCondition.Value=pager.WhereCondition;
myCommand.SelectCommand.Parameters.Add(parameterWhereCondition);
SqlParameterparameterTableName=newSqlParameter("@TableName",SqlDbType.NVarChar,500);
parameterTableName.Value=pager.TableName;
myCommand.SelectCommand.Parameters.Add(parameterTableName);
SqlParameterparameterOrder=newSqlParameter("@Order",SqlDbType.NVarChar,500);
parameterOrder.Value=pager.Order;
myCommand.SelectCommand.Parameters.Add(parameterOrder);
SqlParameterparameterSelectStr=newSqlParameter("@SelectStr",SqlDbType.NVarChar,500);
parameterSelectStr.Value=pager.SelectStr;
myCommand.SelectCommand.Parameters.Add(parameterSelectStr);
SqlParameterparameterGroupby=newSqlParameter("@Groupby",SqlDbType.NVarChar,100);
parameterGroupby.Value=pager.Groupby;
myCommand.SelectCommand.Parameters.Add(parameterGroupby);
SqlParameterparameterOrderType=newSqlParameter("@OrderType",SqlDbType.Bit);
parameterOrderType.Value=pager.OrderType==false?0:1;
myCommand.SelectCommand.Parameters.Add(parameterOrderType);
DataSetreturnDS=newDataSet();
//SqlDataAdaptersqlDA=myCommand.crnewSqlDataAdapter(myCommand);
myCommand.Fill(returnDS);
pager.PageCount=(int)parameterPageCount.Value;
pager.RecordCount=(int)parameterRecordCount.Value;
returnreturnDS;
}
}
天生分页#region天生分页
/**////<summary>
///天生分页格局
///</summary>
///<paramname="pager"></param>
///<paramname="url"></param>
///<paramname="isBr"></param>
///<returns></returns>
publicstringOutPager(Pagerpager,stringurl,boolisBr)
{
StringBuilderreturnOurWml;
if(isBr)
{
returnOurWml=newStringBuilder("["+pager.PageCount.ToString()+"页,"+pager.RecordCount.ToString()+"条]<br/>");
}
else
{
returnOurWml=newStringBuilder();
}
if(pager.PageMode==PageMode.Num)
{
//分页每行显现的数目
intpagersCount=10;
intpagers=0;
intstartInt=1;
intendInt=pager.PageCount;
inti=1;
stringendStr="";
if(pager.PageCount>pagersCount)
{
//doublek=;
pagers=pager.PageIndex/pagersCount;
if(pagers==0)
{
pagers=1;
}
elseif((pager.PageIndex%pagersCount)!=0)
{
pagers+=1;
}
endInt=pagers*pagersCount;
if(pager.PageIndex<=endInt)
{
startInt=endInt+1-pagersCount;
if(startInt<1)
{
startInt=1;
}
}
//显现数目不敷时pagersCount
if(endInt>=pager.PageCount)
{
endInt=pager.PageCount;
}
else
{
//if(pager.PageIndex)
endStr="<ahref="";
endStr+=url+"&pageIndex="+(endInt+1).ToString()+""title=第"+(endInt+1).ToString()+"页>";
endStr+=">>";
endStr+="</a>";
}
if(pagers>1)
{
returnOurWml.Append("<ahref="");
returnOurWml.Append(url+"&pageIndex="+(startInt-1).ToString()+""title=第"+(startInt-1).ToString()+"页>");
returnOurWml.Append("<<");
returnOurWml.Append("</a>");
}
}
for(i=startInt;i<=endInt;i++)
{
if(i!=pager.PageIndex)
{
returnOurWml.Append("<ahref="");
returnOurWml.Append(url+"&pageIndex="+i.ToString()+""title=第"+i.ToString()+"页>");
returnOurWml.Append("["+i.ToString()+"]");
returnOurWml.Append("</a>");
}
else
{
returnOurWml.Append("<u>"+i.ToString()+"</u>");
}
}
returnOurWml.Append(endStr);
returnreturnOurWml.Append("<br/>").ToString();
}
else
{
if(pager.PageIndex>1)
{
returnOurWml.Append("<ahref="");
returnOurWml.Append(url+"&pageIndex="+(pager.PageIndex-1).ToString()+"">");
returnOurWml.Append("上一页");
returnOurWml.Append("</a>");
}
if(pager.PageIndex<pager.PageCount)
{
returnOurWml.Append(pager.PageIndex.ToString());
returnOurWml.Append("<ahref="");
returnOurWml.Append(url+"&pageIndex="+(pager.PageIndex+1).ToString()+"">");
returnOurWml.Append("下一页");
returnOurWml.Append("</a>");
}
returnreturnOurWml.Append("<br/>").ToString();
}
}
#endregion
}
}
不过你如果学.net的话,你就不要选os了,这课比较底层的。你可以旁听数据库加上软件构件和中间件。(webservices和面向服务的课也应该听一听) |
|