[英]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();
}
}
創建一個單獨的過程,該過程僅返回行數。
檢查該值而不是完全檢索的數據集的行數,然后根據需要檢索完整的數據集。
請記住,您可以使用相同的連接進行兩次檢索,而無需關閉調用之間的連接。
如果確定需要填充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.