Simple data access component with Connection, Command and DataReader Classes in VB.NET

Connection , command and DataReader classes provide you with the fundamental methods of working with database data.
  • 4778

You can build a simple data access component with Connection, command and DataReader classes.

SqlConnection object is used to represent connection to data source.

SqlCommand object is used to represent a SELECT command of SQL Server.

DataReader is used to represent execution of result command.

In this article , EmployeeClass component ,include ALL() method that returns every records from Employee table.


using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Configuration;
using System.Data.SqlClient;
using System.Data;

class EmployeeClass
    private static readonly string Connection_String;
   private int Emp_id;
   private string Emp_Fname;
   private string Emp_lname;
   private string Emp_country;

    public int id
        get { return Emp_id; }
        set { Emp_id = value; }
    public string FirstName
        set { Emp_Fname = value; }
        get { return Emp_Fname; }
    public string LastName
        set { Emp_lname = value; }
        get { return Emp_lname; }
    public string Country
        set { Emp_country = value; }
        get { return Emp_country; }

   public List<EmployeeClass> All()
        List<EmployeeClass> result = new List<EmployeeClass>();
        SqlConnection con = new SqlConnection(Connection_String);
        SqlCommand cmd = new SqlCommand("SELECT id,Emp_fname,Emp_lname,Emp_country,Emp_salary  FROM Employee",con);
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
                EmployeeClass emp = new EmployeeClass();
        = (int)reader["id"];
                 emp.FirstName = (string)reader["Emp_fname"].ToString();
                 emp.LastName = (string)reader["Emp_lname"].ToString(); 
                 emp.Country = (string)reader["Emp_country"].ToString(); 
        return result;
          static EmployeeClass()
        Connection_String = WebConfigurationManager.ConnectionStrings["MasterDatabase"].ConnectionString;         


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Main.aspx.cs" Inherits="Main" 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">%>
<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="ObjectDataSource1">
                <asp:BoundField DataField="id" HeaderText="id" SortExpression="id" />
                <asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" />
                <asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" />
                <asp:BoundField DataField="Country" HeaderText="Country" SortExpression="Country" />
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:MasterDatabase %>"            SelectCommand="SELECT * FROM [Employee]"></asp:SqlDataSource>
       <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="All"



More Articles

© 2020 DotNetHeaven. All rights reserved.