ASP.Net + SQL Server 存储过程实现分页排序
问题提出:
在应用程序中经常需要查询数据。当查询结果数据量比较大的时候,检索结果、界面显示都需要花费大量的时间。为了避免这个问题,应该每次只检索部分数据,也就是使用常见的分页方式来处理。分页的问题在asp.net中好像非常简单,只要在GridView中启用分页就可以了。启用分页后,GridView关联数据源控件,依旧会加载所有的数据。这个解决方法只是“掩耳盗铃” ,会导致在大数据量的情况下导致查询的效率变低。
解决方法:
使用GridView的自定义分页功能。使用自定义分页功能需要实现两个逻辑:得到结果集的总数、查找自定范围的数据。
GridView 分页实现,可以参考 Scott Mitchell 文章 Efficiently Paging Through Large Amounts of Data
本文组要介绍如何在使用SQL Server 实现排序获取分页数据。
1、使用子查询+TOP关键字方式
if object_id('GetStudentPaged') is not null
drop procedure GetStudentPaged;
go
/************************************
* 描述: 查找指定范围的记录
* 参数:@startRow 其实记录
* @maximumRows 最大的记录数量
*************************************/
create procedure GetStudentPaged
@startRow int,
@maxmimumRows int
as
select top (@maxmimumRows) * from Student
where StudentId NOT IN( select top(@startRow) StudentId from Student )
go
如果需要按条件查找,条件可能会有所不同。通过参数将where条件传入到存储过程中。由于where子句不支持使用变量,所以需要将在存储过程中组合SQL语句,通过动态SQL方式执行。
create procedure GetStudentPaged
@startRow int,
@maxmimumRows int,
@whereExpression nvarchar(512)
as
--存储SQL语句的字符串
declare @sql nvarchar(max)
set @sql = N'Select Top(' + ltrim(str(@maxmimumRows)) + N') * '
set @sql = @sql + N' from Student where '
--判断是否有查询条件
if @whereExpression is not null and @whereExpression <> N''
begin
set @sql = @sql + @whereExpression + N' and '
end
set @sql = @sql + N'StudentId NOT IN ('
set @sql = @sql + N'Select Top('+ ltrim(str(@
相关文档:
常见并发并发一致性问题包括:丢失的修改、不可重复读、读脏数据、幻影读(幻影读在一些资料中往往与不可重复读归为一类)。
2.2.1.1 丢失修改
下面我们先来看一个例子,说明并发操作带来的数据的不一致性问题。
考虑飞机订票系统中的一个活动序列:
甲售票点(甲事务)读出某航班的机票余额A,设A=16.
乙售票点(乙事务 ......
string error_syntaxfromSQL, error_create
string new_sql, new_syntax
new_sql = 'SELECT emp_data.emp_id, ' &
& ......
下面是我总结出来的一个数据库访问公共类,基于ADO.NET,C#的,其中,以重载的方式实现使用存属过程的接口和不用存储过程的接口,如有不妥请大家指正,谢谢~
作者:shinehoo
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace shineh ......
using System.Web;
/// <summary>
/// Javascript常用方法
/// </summary>
public class JS
{
private static string ScriptStart = "<script type=\"text/javascript\">";
private static string ScriptEnd = "</script>";
&n ......