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

Nat*_*izz 5 c# sql asp.net

我有大量的数据(一个包含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>
Run Code Online (Sandbox Code Playgroud)

如您所见,我已启用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();
Run Code Online (Sandbox Code Playgroud)

如何提高性能?

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

Jup*_*aol 7

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

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

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

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

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

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

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

g.DataSource = myContext.MyTable.Skip(pageIndex * pageSize).Take(pageSize);
g.DataBind();
Run Code Online (Sandbox Code Playgroud)

使用示例 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>
Run Code Online (Sandbox Code Playgroud)

数据组件

[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();
    }
}
Run Code Online (Sandbox Code Playgroud)