This tutorial will show you how to build your own
Data Access Component and how to retrieve the time taken to execute. C#
version.
Building DAC with Execution Time in ASP.NET 3.5 and C#
This tutorial will show you how to build your own
Data Access Component and how to retrieve the time taken to execute. C#
version.
Instead of using ASP.NET's built-in controls to retrieve our
data, we can build our own Data Access Components. In this tutorial, we
will look at how we can do this, and how we can also retrieve the
duration of data retrieval. This can be useful when dealing with large
amounts of data. For this example, we will be working with a SQL
database, and create our own class to retrieve the data. We will be
using the Object Data Source to interact with our class, and a GridView
to display the data.
The database we will be working with will have just one table, and
three columns - id, name and age. Once created, we will add some sample
data to use.
We used over 10 web hosting companies before we found Server Intellect. Their dedicated servers
and add-ons were setup swiftly, in less than 24 hours. We were able to
confirm our order over the phone. They respond to our inquiries within
an hour. Server Intellect's customer support and assistance are the best we've ever experienced.
When our database is ready to go, we can start building our class
that will retrieve data from our database. The class will have a method
to retrieve all records from the database and input into a List. Our
class will look something like this:
public class People
{
private static readonly string _connectionString;
private string _name;
private string _age;
public string Name
{
get
{
return _name;
}
set
{
_name = value;
}
}
public string Age
{
get
{
return _age;
}
set
{
_age = value;
}
}
public List<People> GetAll(out long execTime)
{
List<People> results = new List<People>();
SqlConnection con = new SqlConnection(_connectionString);
SqlCommand cmd = new SqlCommand("SELECT Name,Age FROM tblPeople", con);
con.StatisticsEnabled = true;
using (con)
{
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
People newPerson = new People();
newPerson.Name = (string)dr["Name"];
newPerson.Age = (string)dr["Age"];
results.Add(newPerson);
}
}
IDictionary stats = con.RetrieveStatistics();
execTime = (long)stats["ExecutionTime"];
return results;
}
static People()
{
_connectionString = WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
}
} |
If you're looking for a really good web host, try Server Intellect - we found the setup procedure and control panel, very easy to adapt to and their IT team is awesome!
The GetAll method uses a List to collect all data from the database
and then using a loop, we add each record from the database into the
results List we created. This is the List that is then returned. We
will use this method to select data, using the ObjectDataSource. We can
now build our ASPX page like so:
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server" DataSourceID="ObjectDataSource1"
Width="377px" />
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
TypeName="People" SelectMethod="GetAll" OnSelected="ObjectDataSource1_Selected">
<SelectParameters>
<asp:Parameter Name="execTime" Type="Int64" Direction="Output" />
</SelectParameters>
</asp:ObjectDataSource>
<br />
<br />
Time to retrieve data was: <asp:Label ID="lblStatus" runat="server" />.
</form> |
Notice that we have assigned the GetAll method to our SelectMethod
attribute of our ObjectDataSource, and the TypeName is the name of the
Class. We also have a method that fires on the OnSelected event. This
means that when the ObjectDataSource selects data (through the class),
the following code is processed:
protected void ObjectDataSource1_Selected(object sender, ObjectDataSourceStatusEventArgs e)
{
lblStatus.Text = e.OutputParameters["execTime"].ToString() + "ms";
} |
We chose Server Intellect for its dedicated servers, for our web hosting. They have managed to handle virtually everything for us, from start to finish. And their customer service is stellar.
This is where we output the time it took to retrieve the data.
The entire code-behind will look something like this:
using System;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void ObjectDataSource1_Selected(object sender, ObjectDataSourceStatusEventArgs e)
{
lblStatus.Text = e.OutputParameters["execTime"].ToString() + "ms";
}
} |
The entire code of the class is as follows:
using System;
using System.Data;
using System.Web.Configuration;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Collections;
/// <summary>
/// Summary description for People
/// </summary>
public class People
{
private static readonly string _connectionString;
private string _name;
private string _age;
public string Name
{
get
{
return _name;
}
set
{
_name = value;
}
}
public string Age
{
get
{
return _age;
}
set
{
_age = value;
}
}
public List<People> GetAll(out long execTime)
{
List<People> results = new List<People>();
SqlConnection con = new SqlConnection(_connectionString);
SqlCommand cmd = new SqlCommand("SELECT Name,Age FROM tblPeople", con);
con.StatisticsEnabled = true;
using (con)
{
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
People newPerson = new People();
newPerson.Name = (string)dr["Name"];
newPerson.Age = (string)dr["Age"];
results.Add(newPerson);
}
}
IDictionary stats = con.RetrieveStatistics();
execTime = (long)stats["ExecutionTime"];
return results;
}
static People()
{
_connectionString = WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
}
} |
|