的分页排序,而且这个表的数据量比较大。就很难使用 DataSet 来进行。这样我们可以使用 临时存储过程 来实现这类要求。
一个表:
Create Table T_users
(
??? F_id int IDENTITY(1,1) not null,
??? F_userName nvarchar(50) not null,
??? F_age int,
??? F_UID nvarchar(18)
)
要求:用一个分页程序显示用户的信息,点击不同的字段进行排序分页。
public class DataProvider
{
protected UserCollection Data_UserPages(string column_name,int pageIndex,int pageSize,bool desc)
{
UserCollection users = new UserCollection();
User user;
SqlConnection oConn = new SqlConnection (ConnectionString)
SqlCommand oComm = new SqlCommand();
oConn.open ();
oComm.Connection =oConn;
oComm.CommandType = CommandType.Text ;
oComm.CommandText =
"Create PROCEDURE #userPage\n
AS\n
Declare @PageLowerBound int\n
Declare @PageUpperBound int\n
Declare @AllPage int\n
set @AllPage=0\n
set @PageLowerBound = "+ (pageIndex * pageSize).ToString() +"\n
set @PageUpperBound = "+ (pageIndex * pageSize + pageSize +1 ).ToString() +"1\n
Create Table #PageIndex\n
(\n
IndexId int Identity (1,1) not null,\n
userId int\n
)\n
insert into #PageIndex (userId)\n
select F_id from T_users order by "+ column_name +" "+ desc?"desc":"asc" +"\n
select @AllPage= count from #PageIndex\n
select F_id,F_userName,F_age,F_age,F_UID\n
from T_users P(nolock), #PageIndex PageIndex\n
where P.F_id = PageIndex.companytId and \n
PageIndex.IndexId > @PageLowerBound and \n
PageIndex.IndexId < @PageUpperBound \n
order by PageIndex.IndexId ASC\n
drop Table #PageIndex\n
return @AllPage\n
go\n";
oComm.ExecuteNonQuery();
SqlCommand oCommPage = new SqlCommand();
oCommPage.Connection = oConn;
oComm.CommandType = CommandType.StoredProcedure ;
oCommPage.CommandText ="#userPage";
oCommPage.Parameters.Add("@ReturnValue",SqlDbType.Int );
oCommPage.Parameters["@ReturnValue"].Direction = ParameterDirection.ReturnValue ;
SqlDataReader oReader =oCommPage.ExecuteReader(CommandBehavior.CloseConnection);
while (oReader.read())
{
user = new User();
user.Id = (int) oReader["F_id"];
user.UserName = (string) oReader["F_UserName"];
user.age = (int) oReader["F_age"];
user.UID = (string) oReader["F_UID"];
users.add(user);
}
oReader.Close();
users.AllCount = (int) oCommPage.Parameters["@ReturnValue"].value; // 这是共有多少用户
oConn.Close(); // 关闭连接会把这个临时存储过程自动删除
return users;
}
}
应该还有更好的方法,希望得到高手们指点。
浙公网安备 33010602011771号