我从数据库中提取数据,一张表约500W条数据,有19列, 按人员标识来查询的话正常2秒能出来结果 ,如果select *来绑定到datagridview的话,就会连接超时,但有时也能查出结果来.  因为一些需求  不用分页和虚拟模式显示,求一些 解决超时问题的经验?

解决方案 »

  1.   

    select 需要的字段
    添加索引
      

  2.   

    设置SqlDataAdapter变量的SelectCommand.CommandTimeout = 0;意思是不限制查询命令执行时间。默认查询命令时有超时限制的
      

  3.   

    这种需要分页代码了。不适合采用系统自带的 datagrid的分页功能
    一是内存占用大,二是网络负载也大,三是CPU也受不了,四是数据库的IO也难受
      

  4.   

    建议在数据库那边做存储过程分页:

    create proc PageNum
    @PageSize int, --每页显示条数
    @PageIndex int, --当前是第几页
    @totalRows int output, --总行数
    @totalPages int output --总页数
    as
    declare @startId int
    declare @endId int
    set @startId = @PageSize*(@PageIndex-1)+1
    set @endId = @startId+@PageSize-1
    select @totalRows=COUNT(*) from Books
    set @totalPages=@totalRows/@PageSize
    if(@totalRows%@PageSize != 0) --如果总行数除每页显示数据量的值不等于0,则总页数得加1
    begin
    set @totalPages=@totalPages+1
    end
    declare @IndexTable table(Id int identity(1,1) ,nId int)
    insert into @IndexTable select Id from Books
    select * from @IndexTable as t ,Books as b where t.Id>=@startId and t.Id<=@endId and t.nId=b.Id
    go
     
    declare @tr int --总行数
    declare @tp int --总页数
    exec PageNum 4,2,@tr output ,@tp output
    print '这是总页数'+convert(varchar,@tp) --数据转换
    print @tr