May 26, 2016 23:35 by
Peter
It is not smart to execute multiple db request for loading single page. Review your database code to see if you have got request paths that go to the database quite once. each of these round-trips decreases the number of requests per second your application will serve. By returning multiple resultsets in a single database request, you can cut the total time spent communicating with the database.
In order to enhance performance you should execute single keep proc and bring multiple resultset in to single dB request. In this article i will explain you how to avoid multiple database request and how to bring multiple resultset into single dB request. Consider a scenario of loading a Product Page, which displays:
Product information
Product Review information.
In order to bring two information request in single dB request, your sql server keep proc ought to be declared as below.
SQL Server Stored Proc
CREATE PROCEDURE GetProductDetails
@ProductId bigint,
AS
SET NOCOUNT ON
--Product Information
Select ProductId,
ProductName,
ProductImage,
Description,
Price
From Product
Where ProductId = @ProductId
--Product Review Information
Select ReviewerName,
ReviewDesc,
ReviewDate
From ProductReview
Where ProductId = @ProductId
Asp.net, C# Code to bring multiple db request into single db request
Code Inside Data Access Class Library (DAL)
public DataSet GetProductDetails()
{
SqlCommand cmdToExecute = new SqlCommand();
cmdToExecute.CommandText = "GetProductDetails";
cmdToExecute.CommandType = CommandType.StoredProcedure;
DataSet dsResultSet = new DataSet();
SqlDataAdapter adapter = new SqlDataAdapter(cmdToExecute);
try
{
var conString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnStr"];
string strConnString = conString.ConnectionString;
SqlConnection conn = new SqlConnection(strConnString);
cmdToExecute.Connection = conn;
cmdToExecute.Parameters.Add(new SqlParameter("@ ProductId", SqlDbType.BigInt, 8, ParameterDirection.Input, false, 19, 0, "", DataRowVersion.Proposed, _productId));
//Open Connection
conn.Open();
// Assign proper name to multiple table
adapter.TableMappings.Add("Table", "ProductInfo");
adapter.TableMappings.Add("Table1", "ProductReviewInfo");
adapter.Fill(dsResultSet);
return dsResultSet;
}
catch (Exception ex)
{
// some error occured.
throw new Exception("DB Request error.", ex);
}
finally
{
conn.Close();
cmdToExecute.Dispose();
adapter.Dispose();
}
}
Code Inside Asp.net .aspx.cs page
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString[ProductId] != null)
{
long ProductId = Convert.ToInt64(Request.QueryString[ProductId].ToString());
DataSet dsData = new DataSet();
//Assuming you have Product class in DAL
ProductInfo objProduct = new ProductInfo();
objProduct.ProductId = ProductId;
dsData = objProduct.GetProductDetails();
DataTable dtProductInfo = dsData.Tables["ProductInfo"];
DataTable dtProductReviews = dsData.Tables["ProductReviewInfo"];
//Now you have data table containing information
//Make necessary assignment to controls
.....
.....
.....
.....
.....
}
}
HostForLIFE.eu SQL Server 2014 Hosting
HostForLIFE.eu is European Windows Hosting Provider which focuses on Windows Platform only. We deliver on-demand hosting solutions including Shared hosting, Reseller Hosting, Cloud Hosting, Dedicated Servers, and IT as a Service for companies of all sizes.