GridView问题超过1000条记录

时间:2015-06-22 18:15:55

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

当我返回超过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();
    }
}

1 个答案:

答案 0 :(得分:0)

  1. 创建一个只返回行数的单独程序 检查 值不是完全检索数据集的行数,然后根据需要检索完整数据集。

  2. 请注意,您可以使用相同的连接进行两次检索,无需关闭来电之间的连接。

  3. 如果您确定需要填写gridview,并且无需编辑数据,则可以在不使用适配器的情况下读入DataTable。以下是使用using语句或try / catch修改的基本概念:

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