我刚刚在我的webforms
项目中使用C#
和asp.net
接管了一个项目。我的页面加载时间非常慢,我认为这是由于正在进行的多个数据库调用,但我不知道如何以不同的方式执行此操作。例如,我有一个页面,其中包含3个不同的dropdownlists
每个dropdownlist
填充在Page_Load()
事件处理程序中,但所有3个都有自己的数据库调用。
下面是pseducode以显示正在使用的方法。完成这样的事情的正确方法是什么?
namespace CEDS
{
public partial class BBLL : System.Web.UI.UserControl
{
private DataSet DS = new DataSet();
private C2 _C2 = new C2();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GetDataForDropDown1();
GetDataForDropDown2();
GetDataForDropDown3();
}
}
private void GetDataForDropDown1()
{
DS = _C2.GetDataForDropDown1();
this.gv1.DataSource = DS;
this.gv1.DataBind();
this.gv1.Visible = true;
}
private void GetDataForDropDown2()
{
DS = _C2.GetDataForDropDown2();
this.gv2.DataSource = DS;
this.gv2.DataBind();
this.gv2.Visible = true;
}
private void GetDataForDropDown3()
{
DS = _C2.GetDataForDropDown3();
this.gv3.DataSource = DS;
this.gv3.DataBind();
this.gv3.Visible = true;
}
}
public class C2
{
private DataSet DS = new DataSet();
private DatabaseAccessLayer DAL = new DatabaseAccessLayer();
public DataSet GetDataForDropDown1()
{
DS = new DataSet();
DAL.SqlQueryBuilder = new StringBuilder();
DAL.SqlQueryBuilder.Append("exec dbo.RunStoredProcedure1 ");
DS = DAL.ExecuteSqlQuery(databaseConnection, DAL.SqlQueryBuilder.ToString());
return DS;
}
public DataSet GetDataForDropDown2()
{
DS = new DataSet();
DAL.SqlQueryBuilder = new StringBuilder();
DAL.SqlQueryBuilder.Append("exec dbo.RunStoredProcedure2 ");
DS = DAL.ExecuteSqlQuery(databaseConnection, DAL.SqlQueryBuilder.ToString());
return DS;
}
public DataSet GetDataForDropDown3()
{
DS = new DataSet();
DAL.SqlQueryBuilder = new StringBuilder();
DAL.SqlQueryBuilder.Append("exec dbo.RunStoredProcedure3 ");
DS = DAL.ExecuteSqlQuery(databaseConnection, DAL.SqlQueryBuilder.ToString());
return DS;
}
}
public class DatabaseAccessLayer
{
public DataSet ExecuteSqlQuery(string connectionString, string sqlQuery)
{
try
{
_connectionString = System.Configuration.ConfigurationManager.AppSettings[connectionString].ToString();
_sqlDatabaseConnection = new SqlConnection(_connectionString);
_sqlCommand = new SqlCommand(sqlQuery, _sqlDatabaseConnection);
_sqlDatabaseConnection.Open();
_sqlCommand.CommandTimeout = 0;
_dataSet = new DataSet();
_sqlDataAdapter = new SqlDataAdapter(_sqlCommand);
_sqlDataAdapter.Fill(_dataSet, "Data");
return _dataSet;
}
catch (Exception exception) { throw exception; }
finally
{
_sqlDatabaseConnection.Close();
_sqlCommand.Dispose();
_sqlDataAdapter.Dispose();
}
}
}
}
答案 0 :(得分:2)
你应该能够做到
Parallel.Invoke(GetDataForDropDown1, GetDataForDropDown2, GetDataForDropDown3);
所以至少你不等第一个完成,直到你开始等待第二个和第三个。
使用单个存储过程返回所有三个记录集可能更有效,因此您的数据库连接和检索往返只能一次。但这可能意味着您必须更改数据层代码。
答案 1 :(得分:2)
Page_Load()
{
var t1 = GetDataForDropDown1();
var t2 = GetDataForDropDown2();
var t3 = GetDataForDropDown3();
await Task.WhenAll(t1, t2, t3);
PopulateDD1();
PopulateDD2();
PopulateDD3();
}
async Task GetDataForDropDown1()
{
SqlQuery
Call To Database Access Layer
await Execute Stored Procedure
Store Returned Result In Dataset
}
async Task GetDataForDropDown2()
{
SqlQuery
Call To Database Access Layer
await Execute Stored Procedure
Store Returned Result In Dataset
}
async Task GetDataForDropDown3()
{
SqlQuery
Call To Database Access Layer
await Execute Stored Procedure
Store Returned Result In Dataset
}
答案 2 :(得分:1)
更好的方法可能就是这样,试试吧。
Page_Load()
{
if(!Page.IsPostBack)
{
LoadData();
}
}
LoadData()
{
// PopulateDropDown1 Code
// PopulateDropDown2 Code
// PopulateDropDown3 Code
}
if(!Page.IsPostBack)
阻止LoadData()
在每次回发时都会调用。