标签:dir 数据 store 索引 asc adapter ams rtmp except
Alter Proc P_Page
(
@TblName varchar(200), --表名
@PageSize int, --每页显示条数
@PageIndex int = 1, --页面索引(页码)
@strGetFields varchar(1000)=‘*‘, --无传入值,则搜索所有列
@OrderType int = 0, --是否倒序(0=否,1=是)
@FldName varchar(100), --以什么条件进行倒叙
@StrWhere varchar(2000), --查询条件
@Total int = 0 out
)
As
Begin
Set Nocount on
Declare @strSql varchar(2000)
Declare @strTmp varchar(110)
Declare @strOrder varchar(400)
Set @strSql=‘Select * from [‘ + @TblName + ‘] where 1 = 1‘ +@StrWhere
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
If @PageIndex = 1
Begin
set @strSQL = ‘select top ‘ + rtrim(ltrim(str(@PageSize))) +‘ ‘+@strGetFields+ ‘ from [‘ + @tblName + ‘] where 1=1‘ + @strWhere + ‘ ‘ + @strOrder
end
Else
Begin
Set @strSQL = ‘select top ‘ + str(@PageSize) +‘ ‘+@strGetFields+ ‘ 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) +‘ ‘+@strGetFields+ ‘ from [‘
+ @tblName + ‘] where [‘ + @fldName + ‘]‘ + @strTmp + ‘([‘
+ @fldName + ‘]) from (select top ‘ + str((@PageIndex-1)*@PageSize) + ‘ [‘
+ @fldName + ‘] from [‘ + @tblName + ‘] where 1=1‘ + @strWhere + ‘ ‘
+ @strOrder + ‘) as tblTmp) and 1=1‘ + @strWhere + ‘ ‘ + @strOrder
End
Exec (@strSQL)
Set Nocount off
End
Exec P_Page‘表名‘,‘显示数据量‘,‘页码‘,‘搜索列‘,0,‘正倒序列名‘,‘查询条件‘
调用存储过程
//调用方法
public static DataTable GetRecordByPage(string tblName, int PageSize, int PageIndex, string strWhere)
{
return GetRecordByPage(tblName, PageSize, PageIndex, "*", 0, "Id", strWhere);
}
//传值给参数
public static DataTable GetRecordByPage(string tblName, int PageSize, int PageIndex, string strGetFields,int OrderType, string fldName, string strWhere)
{
DataTable ds = new DataTable();
try
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
SetParams(cmd);
cmd.Parameters[0].Value = tblName;
cmd.Parameters[1].Value = strGetFields;
cmd.Parameters[2].Value = fldName;
cmd.Parameters[3].Value = PageSize;
cmd.Parameters[4].Value = PageIndex;
cmd.Parameters[5].Value = OrderType;
cmd.Parameters[6].Value = strWhere;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "P_Page";
cmd.CommandTimeout = 180;
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = cmd;
DataSet source = new DataSet();
adapter.Fill(ds);
object o = cmd.Parameters["@Total"].Value;
}
}
catch (SqlException e)
{
throw e;
}
return ds;
}
//建立参数
private static void SetParams(SqlCommand cmd)
{
cmd.Parameters.Add(new SqlParameter("@tblName", SqlDbType.VarChar, 255));
cmd.Parameters.Add(new SqlParameter("@strGetFields", SqlDbType.VarChar, 1000));
cmd.Parameters.Add(new SqlParameter("@fldName", SqlDbType.VarChar, 255));
cmd.Parameters.Add(new SqlParameter("@PageSize", SqlDbType.Int));
cmd.Parameters.Add(new SqlParameter("@PageIndex", SqlDbType.Int));
cmd.Parameters.Add(new SqlParameter("@OrderType", SqlDbType.Bit));
cmd.Parameters.Add(new SqlParameter("@strWhere", SqlDbType.VarChar, 1500));
SqlParameter param = new SqlParameter("@Total", SqlDbType.Int);
param.Direction = ParameterDirection.Output;
cmd.Parameters.Add(param);
}
标签:dir 数据 store 索引 asc adapter ams rtmp except
原文地址:http://www.cnblogs.com/illegal/p/6374339.html