Avoid database round trips when we have more than one user control on aspx page
I have more than one user control on my aspx page, and each user control call it's own service or method to display content retrieved from SQL DB. I want to reduce these round trips, and get the job done in one call. A开发者_如何学Cny idea what's the best practice.
You can put your all select statements in one stored procedure that mean you will hit your database just one time and load the result into a DataSet
using SqlAdapter.Fill(MyDataSet)
and pass the each table in your dataset to corresponding user control by creating public property with DataTable
type
public class UserControl_1
{
private DataTable _dbTable;
public DataTable dbTable;
{
get
{
return _dbTable;
}
set
{
_dbTable = value
}
}
}
in your page
Private DataSet myDataSet;
public void Page_Load(Object sender,EventArgs e)
{
myDataSet = PopulateData(); // call your stored procedure inside it
UserControl_1.dbTable = myDataSet.Tables[0];
UserControl_2.dbTable = myDataSet.Tables[1];
//... and so on
}
I wish that give you a good idea
精彩评论