Execution Time using Data Access Component in C#
See more tutorials in Advanced. This post has No Comments.
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.
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
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; } } |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<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:
1 2 3 4 |
protected void ObjectDataSource1_Selected(object sender, ObjectDataSourceStatusEventArgs e) { lblStatus.Text = e.OutputParameters["execTime"].ToString() + "ms"; } |
This is where we output the time it took to retrieve the data.
The entire code-behind will look something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
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 follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
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; } } |