繁体   English   中英

超过1000条记录的GridView问题

[英]GridView issue with more than 1000 records

当我返回一个包含1000多个记录的记录集时,我遇到了一些性能问题。

有时记录超过2,100,但可能低至10。

通过选择所有记录,我可以执行一些批量操作。

但是,当数量较少时,Gridview很好。 当记录数大于500时,我会在页面上看到性能问题。

我要发生的事情是:如果有500条以上的记录,请不要显示网格 ,而是在页面上显示一个导出为CSV或其他控件的下载按钮。

我的问题:即使我告诉它不显示网格,而是显示一条消息和一个按钮,性能仍然很慢。

下面是我的用于填充GridView的C#代码。 删除了一些不重要的东西,以帮助提高可读性。

如何调整C#代码以获得更好的性能?

SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["ConnectString"].ToString());
SqlCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "SomeProcedure";
cmd.Parameters.Add(SearchParam);

try {
    DataTable GridData = new DataTable();

    conn.Open();
    using(SqlDataAdapter Sqlda = new SqlDataAdapter(cmd)) {
        Sqlda.Fill(GridData);
    }

    if (GridData.Rows.Count == 0) {
        lblSearchMsg.Text = "No Fee Records are in the Queue at this time.";
    } else {


        if (GridData.Rows.Count > 500) {
            lblSearchMsg.Text = "More than " + gridLimit.ToString() + " records returned.";
            //Show the download button

        } else {
            //Persist the table in the Session object. (for sorting)
            Session["GridData"] = GridData;

            lblRowCount.Text = "Count: " + GridData.Rows.Count.ToString();

            myGridView.DataSource = GridData;
            myGridView.DataBind();
            myGridView.Visible = true;
        }
    }

} catch (Exception ex) {
    //Do the error stuff
} finally {
    if (conn != null) {
        conn.Close();
    }
}
  1. 创建一个单独的过程,该过程仅返回行数。
    检查值而不是完全检索的数据集的行数,然后根据需要检索完整的数据集。

  2. 请记住,您可以使用相同的连接进行两次检索,而无需关闭调用之间的连接。

  3. 如果确定需要填充gridview且无需编辑数据,则无需使用适配器就可以读取到DataTable中。 这是您可以根据需要using语句或try / catch进行修改的基本思想:


    conn = new SqlConnection(connString);
    string query = "SELECT * FROM ....";
    SqlCommand cmd = new SqlCommand(query, conn);
    conn.Open();
    SqlDataReader dr = cmd.ExecuteReader();
    DataTable dt = new DataTable();
    dt.Load(dr);
    GridView1.DataSource = dt;
    GridView1.DataBind();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM