【发布时间】: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();
}
}
【问题讨论】:
-
当您说批量操作时,您是对所有记录应用某种值还是对每条记录逐一更改?
-
大部分时间我会对所有记录应用相同的操作。但取决于记录的性质,取决于操作。 (即批准、拒绝、暂停...)不会总是相同的操作。
-
您的
SqlConnection和SqlCommand都需要在using块中。 -
你检索数据的存储过程性能好不好?
-
嗯。我不是一个 SQL 人。我的伙伴编写了 SQL 过程。我倾向于说不。我们数据库中的一切似乎都很慢。但我对 SQL 的了解还不够,无法确定它是否具有良好的性能。
标签: c# sql asp.net stored-procedures gridview