我正在使用以下數據集循環顯示Razor視圖頁上的數據。此時,我的查詢只返回100條記錄,但當我調用該函數在控制器的視圖頁面上顯示時,它需要將近6-7秒。如何提高mvc數據集循環性能asp.net c#
我該如何提高性能?
在此先感謝
private void testreport(Modeltest model)
{
try
{
System.Data.DataSet ds = null;
if (string.IsNullOrEmpty(model.cls))
{
ds = getRepository.gerreports(model.ddl_1, model.ddl_2, null, null, null);
}
else if (model.cls_idx=="0")
{
ds = getRepository.gerreports(model.ddl_1, model.ddl_2, model.ddl_3, null, null);
}
else
{
ds = getRepository.gerreports(model.ddl_1, model.ddl_2, model.ddl_3, model.cls, null);
}
StringBuilder tbl_h = new StringBuilder();
StringBuilder tbl_m = new StringBuilder();
StringBuilder tbl_e = new StringBuilder();
StringBuilder tbl_a = new StringBuilder();
var data = ds.Tables[0];
foreach (System.Data.DataRow rw in data.Rows)
{
var r1 = rw["cls_T"].ToString();
var r3 = rw["cls_T_3"].ToString();
var r15 = rw["cls_set"].ToString();
var r17 = rw["cls_name"].ToString();
var r18 = rw["cls_p_name"];
var r19 = rw["cls_lbl"];
var r20 = rw["cls_Type"].ToString();
var r22 = rw["cls_Text"];
var r23 = rw["cls_qt"].ToString();
var r23_int = Convert.ToInt32(rw["cls_qt"].ToString());
var r24 = rw["cls_resps"].ToString();
if (r3 == "sc")
{
tbl_h.AppendFormat(@"<table class='table'><thead><tr><th>{0}</th><th>Reports</th><th>Class</th></tr></thead><tbody>", r19);
}
else if (r3 == "cmt")
{
tbl_h.AppendFormat(@"<table class='table1'><thead><tr><th>{0}</th><th>Reports</th></tr></thead><tbody>", r19);
}
if (r20 == "cls_qt" && (r15 == "q_ask" || r15 == "rpt"))
{
if (r24 > 0)
{
if (r23_int >= 75)
{
tbl_m.AppendFormat(@"<tr><td>{0}</td><td>{1}</td><td>{2}</td></tr>", r22, r24, r23);
}
else if (r23_int >= 60 && r23_int < 75)
{
tbl_m.AppendFormat(@"<tr><td>{0}</td><td>{1}</td><td>{2}</td></tr>", r22, r24, r23);
}
else if (r23_int > 0 && r23_int < 50)
{
tbl_m.AppendFormat(@"<tr><td>{0}</td><td>{1}</td><td>{2}</td></tr>", r22, r24, r23);
}
}
else
{
tbl_m.AppendFormat(@"<tr><td>{0}</td><td> </td><td> </td></tr>", r22);
}
}
else if (r20 == "bk" && r17 == "cmt_cat")
{
if (r24 > 0 && r24 >= 100)
{
tbl_m.AppendFormat(@"<tr><td><a href='{0}'>{0}</a></td><td>{1}</td></tr>", r18, r24);
}
else if (r24 > 0 && r24 < 100)
{
tbl_m.AppendFormat(@"<tr><td><a href='{0}'>{0}</a></td><td>{1}</td></tr>", r18, r24);
}
else
{
tbl_m.AppendFormat(@"<tr><td>{0}</td><td> </td></tr>", r18);
}
}
if (r1 == "bfk")
{
tbl_e.AppendFormat(@"</tbody></table>");
}
tbl_a.AppendFormat(tbl_h.ToString() + tbl_m.ToString() + tbl_e.ToString());
tbl_h.Clear();
tbl_m.Clear();
tbl_e.Clear();
}
ViewBag.mreport = tbl_a.ToString();
}
catch (Exception ex)
{
throw new ApplicationException("report: ", ex);
}
}
我的SQL執行功能 -
private DataSet ExeSet()
{
string sql = "select * from reportTest('test2017', 'Jan 2017', null, null, null) order by ID"
try
{
DataSet ds = new DataSet();
using (SqlConnection con = new SqlConnection(GetCon()))
using (SqlCommand cmd = new SqlCommand(sql, con))
{
con.Open();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
da.Fill(ds);
return ds;
}
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
return null;
}
}
使用一個分析器,並查看大部分時間需要的東西。我的直覺告訴我,當你通過第一個元素循環時,這條線會受到打擊:'foreach(Data.Rows中的System.Data.DataRow rw)' –
@QualityCatalyst,我認爲你是對的,但我怎麼能改善這一點,只是我改變了以下 - 'DataTableReader dr = ds.CreateDataReader(); (dr.Read())'但是沒有運氣 –
@QualityCatalyst,我該如何改進它 –