优化加载和搜索速度ASP.NET C#
我对ASP.NET完全是新手,但是经过一些基础知识的学习,多亏了谷歌,我成功地建立了一个页面来满足我的需要 Web应用程序应该从我用本地主机上的另一个Windows应用程序生成的XML文件中加载数据,显示这些数据并允许用户搜索 此XML文件的大小超过50 MB,条目数超过120.000 我将这个XML文件读入数据集,然后绑定到gridView 问题是:优化加载和搜索速度ASP.NET C#,c#,asp.net,xml,performance,dataset,C#,Asp.net,Xml,Performance,Dataset,我对ASP.NET完全是新手,但是经过一些基础知识的学习,多亏了谷歌,我成功地建立了一个页面来满足我的需要 Web应用程序应该从我用本地主机上的另一个Windows应用程序生成的XML文件中加载数据,显示这些数据并允许用户搜索 此XML文件的大小超过50 MB,条目数超过120.000 我将这个XML文件读入数据集,然后绑定到gridView 问题是: 当我第一次加载页面时,可能需要30秒 当我搜索时,加载数据可能需要10秒以上 我怎样才能解决这个问题? 我尝试了StateView,但是这导
- 当我第一次加载页面时,可能需要30秒
- 当我搜索时,加载数据可能需要10秒以上
public DataSet ds = new DataSet();
public DataSet resultDS = new DataSet();
public bool searchListActive = false;
string _sortDirection = "";
protected void Page_Load(object sender, EventArgs e)
{
BindGrid();
if (!IsPostBack)
{
gridView_IndexData.PageSize = Convert.ToInt32(ddList_DataCount.SelectedItem.Value);
ViewState["searchListActive"] = false;
BindCB();
}
gridView_IndexData.PageSize = Convert.ToInt32(ddList_DataCount.SelectedItem.Value);
}
void BindGrid()
{
ds.ReadXml(Server.MapPath("~/lstData.xml"));
gridView_IndexData.DataSource = ds;
gridView_IndexData.DataBind();
}
void BindCB()
{
DataTable dt = ds.Tables[0].DefaultView.ToTable(true, "forumName");
var DR = dt.NewRow();
DR["forumName"] = "All forums";
dt.Rows.InsertAt(DR, 0);
dt.AcceptChanges();
ddList_Forum.DataSource = dt;
ddList_Forum.DataTextField = "forumName";
ddList_Forum.DataBind();
}
protected void btnSearchQuery_Click(object sender, EventArgs e)
{
resultDS = ds.Clone();
string searchQuery = "";
searchQuery = "TopicTitle LIKE '%" + tbSearchInput.Text + "%'";
if (tbSearchByUsername.Text.Length > 0)
{
searchQuery += "AND UserName ='" + tbSearchByUsername.Text + "'";
}
if (ddList_Type.Text != "")
{
searchQuery += "AND Type ='" + ddList_Type.Text + ":'";
}
if (ddList_Forum.Text != "All forums")
{
searchQuery += "AND forumName ='" + ddList_Forum.Text + "'";
}
var results = ds.Tables[0].Select(searchQuery);
resultDS.Tables.Add();
foreach (DataRow dr in results)
{
resultDS.Tables[0].ImportRow(dr);
}
resultDS.AcceptChanges();
gridView_IndexData.DataSource = resultDS.Tables[0];
ViewState["searchListActive"] = true;
ViewState["resultDS"] = resultDS;
gridView_IndexData.DataBind();
}
protected void gridView_IndexData_Sorting(object sender, GridViewSortEventArgs e)
{
SetSortDirection(e.SortDirection.ToString());
ds.Tables[0].DefaultView.Sort = e.SortExpression + " " + _sortDirection;
gridView_IndexData.DataSource = ds.Tables[0];
gridView_IndexData.DataBind();
}
void SetSortDirection(string sortDirection)
{
if (sortDirection == "Descending")
{
_sortDirection = "DESC";
}
else
{
_sortDirection = "ASC";
}
}
protected void gridView_IndexData_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gridView_IndexData.PageIndex = e.NewPageIndex;
if ((bool)ViewState["searchListActive"] == true)
{
gridView_IndexData.DataSource = (DataSet)ViewState["resultDS"];
}
gridView_IndexData.DataBind();
}
我将结合使用适当的xml处理,例如使用xmlreader。下面是一篇文章,对这一点有一些提示()。而且,尽可能最大限度地利用缓存。这里有一些关于()的提示。1)如果您将共享同一个数据集实例,那么当并发用户同时搜索时,它将给您带来错误
2) 要优化搜索速度,请将结构转换为LINQ而不是数据集查询谢谢。我在客户端缓存了整个数据集,这使得回发可以在几秒钟内加载。我还做了其他的改变。我创建了XML模式,并使用lambda表达式来获取主题,而不是使用dataset的Select()。这为在测试中搜索节省了至少一秒钟的时间,而XML模式确实大大减少了加载时间。现在页面的加载时间为30秒。我给了你和沙迪一票,但是我的名声不好。。。