我剛剛接管了項目,使用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();
}
}
}
}
您應該打開SQL事件探查器並加載頁面,然後隔離每個SqlQuery並將它們複製/粘貼到SSMS中。查詢執行時間將讓您看到在這些查詢上花了多少時間,而不是在.Net內部(儘管此規則有一些例外)。如果您也打開執行計劃,它會警告數據上是否缺少索引。 – EvilDr
如何打開SQL事件探查器? –
我只注意到你有SqlQuery *和*一個存儲過程。每個人在做什麼? – EvilDr