【问题标题】:GridView issue with more than 1000 records超过 1000 条记录的 GridView 问题
【发布时间】:2015-06-22 18:15:55
【问题描述】:

当我返回包含超过 1,000 条记录的记录集时,我遇到了一些性能问题。

有时记录超过 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();
    }
}

【问题讨论】:

  • 当您说批量操作时,您是对所有记录应用某种值还是对每条记录逐一更改?
  • 大部分时间我会对所有记录应用相同的操作。但取决于记录的性质,取决于操作。 (即批准、拒绝、暂停...)不会总是相同的操作。
  • 您的SqlConnectionSqlCommand 都需要在using 块中。
  • 你检索数据的存储过程性能好不好?
  • 嗯。我不是一个 SQL 人。我的伙伴编写了 SQL 过程。我倾向于说不。我们数据库中的一切似乎都很慢。但我对 SQL 的了解还不够,无法确定它是否具有良好的性能。

标签: c# sql asp.net stored-procedures gridview


【解决方案1】:
  1. 创建一个仅返回行数的单独过程。
    检查那个值而不是完全检索到的数据集的行数,然后根据需要检索整个数据集。

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

  3. 如果您确定需要填充网格视图并且无需编辑数据,您可以在不使用适配器的情况下将其读入 DataTable。以下是使用using 语句修改的基本思想或根据您的喜好尝试/捕获:


    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();

【讨论】:

    猜你喜欢
    • 2019-04-11
    • 1970-01-01
    • 1970-01-01
    • 2014-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-25
    • 1970-01-01
    相关资源
    最近更新 更多