

Navigator : Home > Tutorials > Advanced Technologies Tutorials > ...
Execution Time of Data Access Component in C#
In this tutorial, you will learn how to create your own class to retrieve data from a database and also how to output how long it took to retrieve that data. C# version.
This tutorial will show how we can build our own Data Access Components and also get how long it took for the data to be retrieved. 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.
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; } } |
Server Intellect assists companies of all sizes with their hosting needs by offering fully configured server solutions coupled with proactive server management services. Server Intellect specializes in providing complete internet-ready server solutions backed by their expert 24/365 proactive support team.
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"; } |
Server Intellect offers Windows Hosting Dedicated Servers at affordable prices. I'm very pleased!
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"; } } |
Need help with Windows Dedicated Hosting? Try Server Intellect. I'm a happy customer!
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; } } |
Looking for more .NET Tutorials? Click Here!