Tutorial RSS
 
Navigator: Home - Advanced - Execution Time using Data Access Component in C#

Execution Time using Data Access Component in C#

This tutorial will show you how to create your own Data Access Component and also how to display the time it takes to retrieve data from a SQL database. C# version.

Server Intellect offers Windows Hosting Dedicated Servers at affordable prices. I'm very pleased!


Looking for the VB.NET 2008 Version? Click Here!

Looking for more .NET Tutorials? Click Here!

Using Visual Studio.NET 2008, we can build our own Data Access Components to work with and manipulate data. In this tutorial, we will look at how we can do this, and how we can also retrieve the duration of data retrieval, which can be useful when dealing with large amounts of data. For this example, we will be working with a SQL database, and we will be creating 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;
}
}

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!


We create a 'GetAll' method that uses a List to collect all data from the database. 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 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.


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;
}
}


Try Server Intellect for Windows Server Hosting. Quality and Quantity!




Looking for the VB.NET 2008 Version? Click Here!

Looking for more .NET Tutorials? Click Here!
Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!



100% SPAM FREE! We will never sell or rent your email address!

411asp.net123aspxDotNetFreaksServer Intellect