28 July 2013

Retrieving Data from SQL Server database (with Stored Procedure) in an ASP.NET MVC 4 Application without using Entity Framework

We have a simple task to retrieve a data from SQL Server database without using Entity Framework (as requested by DBA, lol!) in our ASP.NET MVC application. Here's our simple solution:


Stored Procedure:

CREATE PROC dbo.ListOfKampung

AS

BEGIN

SELECT KampungId, Name, Age, Salary, Score, DateAdded, DateUpdated FROM dbo.KampungTbl

END



Business Layer:

using System;

namespace eKamp.Web.BusinessLogic
{
    public class Kampung
    {
        public Kampung()
        { }

        public Kampung(int kampungId, string name, int age, decimal salary, decimal score, DateTime dateAdded, DateTime dateUpdated)
        {
            KampungId = kampungId;
            Name = name;
            Age = age;
            Salary = salary;
            Score = score;
            DateAdded = dateAdded;
            DateUpdated = dateUpdated;
        }

        public int KampungId { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
        public decimal Salary { get; set; }
        public decimal Score { get; set; }
        public DateTime DateAdded { get; set; }
        public DateTime DateUpdated { get; set; }

    }
}


Data Layer:

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using eKamp.Web.BusinessLogic;

namespace eKamp.Web.Services
{
    public class KampungDataServices
    {
        public static List<Kampung> GetKampungList()
        {
            var results = new List<Kampung>();
            using (var con = new SqlConnection("Data Source=HACK3R;Initial Catalog=KampungDB;Integrated Security=True"))
            {
                using (var cmd = new SqlCommand("ListOfKampung", con))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    try
                    {
                        con.Open();
                        IDataReader reader = cmd.ExecuteReader();
                        while (reader.Read())
                        {
                            var kampung = new Kampung();
                            kampung.KampungId = reader.GetInt32(0);
                            kampung.Name = reader.GetString(1);
                            kampung.Age = reader.GetInt32(2);
                            kampung.Salary = reader.GetDecimal(3);
                            kampung.Score = reader.GetDecimal(4);
                            kampung.DateAdded = reader.GetDateTime(5);
                            kampung.DateUpdated = reader.GetDateTime(6);
                            results.Add(kampung);
                        }
                        reader.Close();
                    }
                    finally
                    {
                        con.Close();
                    }
                }
            }
            return results;
        }
    }
}


Controller:

using System;
using System.Collections.Generic;
using System.Web.Mvc;
using eKamp.Web.Services;
using eKamp.Web.BusinessLogic;

namespace eKamp.Web.Controllers
{
    public class KampungController : Controller
    {
        private List<Kampung> _kampung;
      
        public ActionResult Index()
        {
            _kampung = KampungDataServices.GetKampungList();
            return View(_kampung);
        }
    }
}


No comments:

Post a Comment