asp.net中有关gridview的大量数据

时间:2012-07-18 14:20:42

标签: c# asp.net sql

我有大量数据(带有20000条记录的sql查询),并且使用该数据量填充我的数据网格需要10分钟,这是我的gridview定义:

<asp:GridView ID="g" runat="server" Height="113px" Width="817px" 
BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" 
CellPadding="3" GridLines="Vertical" AllowPaging="True" Font-Size="Small" 
     PageSize="30">
    <AlternatingRowStyle BackColor="#DCDCDC" />
    <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
    <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
    <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
    <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
    <SortedAscendingCellStyle BackColor="#F1F1F1" />
    <SortedAscendingHeaderStyle BackColor="#0000A9" />
    <SortedDescendingCellStyle BackColor="#CAC9C9" />
    <SortedDescendingHeaderStyle BackColor="#000065" />
    <PagerStyle cssClass="gridpager" HorizontalAlign="Left" />  

</asp:GridView>

如您所见,我已启用了AllowPaging属性。

这是我绑定数据的方式:

DataSet dts = new DataSet();
OracleDataAdapter oad = new OracleDataAdapter(query, co.conn);

cmd.CommandText = query;
cmd.CommandType = CommandType.Text;

cmd.CommandText = query;
cmd.CommandType = CommandType.Text;
OracleDataReader reader = cmd.ExecuteReader();

oad.Fill(dts);


g.DataSource = dts.Tables[0];
g.DataBind();

如何改善表现?

当我填充数据集(oad.Fill(dts);)需要10分钟才能完成。这是因为我一次设置了20000条记录吗?有没有办法只显示前30条记录并在用户对网格视图进行分页时调用数据?还有其他方法可以改善表现吗?

3 个答案:

答案 0 :(得分:7)

如果我的理解是正确的,您想要添加服务器分页

当您只是将AllowPaging="True"添加到网格时,默认情况下,GridView不知道如何从服务器分页数据,分页正在内存中执行后已经从数据库中获取了整个结果,每次GridView绑定时都会发生这种情况

我认为你想添加服务器分页(在服务器中分页,只向客户端发送一小串记录),为了做到这一点,你可以利用几个ASP.Net数据源控件。 / p>

由于您手动连接数据库,因此需要在查询中手动添加分页代码并将该代码映射到控件

我认为可以帮助您的唯一数据源控件(因为您使用Oracle作为数据库)是

  • 的SqlDataSource。可悲的是,它不支持服务器分页开箱即用,你需要调整它
  • 的ObjectDataSource。它可以轻松地与GridView控件集成以提供分页,但是您需要手动将代码添加到查询或存储过程以在服务器中对数据进行分页
  • EntityDatasource。在使用EntityFramework
  • 时,它用于连接数据库

如果您将使用EF或NHibernate,它会更容易,代码看起来像:

g.DataSource = myContext.MyTable.Skip(pageIndex * pageSize).Take(pageSize);
g.DataBind();

使用ObjectDatasource

的示例

ASPX

    <asp:ObjectDataSource runat="server" ID="ods" TypeName="MyObject" EnablePaging="True"
        SelectMethod="FindPaging"
        MaximumRowsParameterName="pageSize"
        SortParameterName="sortColumn"
        StartRowIndexParameterName="startIndex"
        SelectCountMethod="FindPagingCount" onselected="ods_Selected"
    >
        <SelectParameters>
            <asp:Parameter Name="sortColumn" Type="String" />
            <asp:Parameter Name="startIndex" Type="Int32" />
            <asp:Parameter Name="pageSize" Type="Int32" />
        </SelectParameters>
    </asp:ObjectDataSource>

            <asp:GridView ID="grid" runat="server" AllowPaging="True" AllowSorting="True" PageSize="10"
                DataSourceID="ods" AutoGenerateColumns="true">
            </asp:GridView>

数据组件

[System.ComponentModel.DataObject]
public class MyObject
{
    [System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Select)]
    public IEnumerable<employee> FindPaging(int startIndex, int pageSize, string sortColumn)
    {
        // place here your code to access your database and use the parameters to paginate your results in the server
    }

    [System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Select)]
    public int FindPagingCount(int startIndex, int pageSize, string sortColumn)
    {
        var c = new DataClassesDataContext();

        return c.employees.Count();
    }
}

答案 1 :(得分:2)

简单的解决方案是抓住前30行,然后当你抓住下一组时,你通过前30来抓住下一个30,其中id&gt; @lastid。

这样你只需要30行,而不必费心从数据库中获取整个记录集。

答案 2 :(得分:2)

分页只是意味着它只显示&#39; pagesize&#39;一次只有一个项目,但它仍然需要从数据库中获取所有数据才能显示这几个项目,因为这是您要查询的内容。

您需要做的是修改查询,使其仅提取&#39; pagesize&#39;项目,然后您需要向OnPageIndexChanging事件添加事件处理程序,以便您可以重新查询数据库以查找下一个&#39; pagesize&#39;项目。 (或者基于你设置的分页选项,或者其他任何东西。事件args将包含用户要求的页面索引。)

如果你在网上搜索&#34; gridview paging&#34;或类似的,您可以找到代码示例,但它们会根据您使用的数据库,您如何获取它等等而有所不同。

为oracle寻呼:

https://stackoverflow.com/a/241643/351383