Wednesday, 29 November 2017

Enterprise Library DAAB - C# .Net

Enterprise Library Data Access Application Block In C# .NET
What is a Data Access Application Block (DAAB)?  A Data Access Application Block encapsulates the performance and resource management best practices for accessing Microsoft SQL Server databases. It can easily be used as a building block in your own .NET-based application. If you use it then you will reduce the amount of custom code you need to create, test, and maintain. It comes with a single assembly with a class that has many useful methods. It reduces the amount of custom code.
A Data Access Application Block provides the following benefits:
  • It uses the functionality provided by ADO.NET 2.0 and with it, you can use ADO.NET functionality along with the application block's functionality.
  • It reduces the need to write boilerplate code to perform standard tasks.
  • It helps maintain consistent data access practices, both within an application and across the enterprise.
  • It reduces difficulties in changing the database type.
  • It relieves developers from learning different programming models for different types of databases.
  • It reduces the amount of code that developers must write when they port applications to different types of databases. Read more in http://msdn.microsoft.com/en-us/library/cc309168.aspx.
Install Enterprise Library
Please follow this link to download the Enterprise Library:
Create a new MVC web application.
Make the below changes in your web.config file.
Add a DAL Folder in your project. Add a Baseclass and add the below code in the baseclass.
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace MVC_ADO.DAL
{
    public class BaseClass
    { 
        public virtual Database GetDatabase()
        {
            Database db;
            db = DatabaseFactory.CreateDatabase("MasterDB");
            return db;
        }      
    }
}
Add a EmployeeModel class and add the below code

using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace MVC_ADO.DAL
{
    public class EmployeeModel : BaseClass
    {
        Database db = null;
        public override Database GetDatabase()
        {
            return base.GetDatabase();
        }

        public DataSet GetEmployee()
        {
            try
            {
                db = GetDatabase();
                DataSet ds = new DataSet();
                DbCommand dbCommand = db.GetStoredProcCommand("PROC_GET_EMPLIST");
                // db.AddInParameter(dbCommand, "@IP_UserID", DbType.Int32, UserID);
                //db.AddOutParameter(dbCommand, "@OP_strException", DbType.String, 200);

                ds = db.ExecuteDataSet(dbCommand);
                return ds;
            }
            catch
            {
                throw;
                // ds = null;
                //strException = ex.Message.ToString();
            }
        }
    }
}
Note: Create the PROC_GET_EMPLIST Stored Procedure in SQL Server.

Now Call the GetEmployee Function from your Controller.


using System.Web.Mvc;
using MVC_ADO.DAL;

namespace MVC_ADO.Controllers
{
    public class HomeController : Controller
    {
        EmployeeModel model = new EmployeeModel();
        public ActionResult Index()
        {
            var list = model.GetEmployee();
            return View();
        }
    }
}
You will get the list of all employees from Employee table.

2 comments:


  1. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a .Net developer learn from Dot Net Training in Chennai. or learn thru Dot Net Training in Chennai. Nowadays Dot Net has tons of job opportunities on various vertical industry.
    or Javascript Training in Chennai. Nowadays JavaScript has tons of job opportunities on various vertical industry.

    ReplyDelete

Topics

ADO .Net (2) Ajax (1) Angular Js (17) Angular2 (24) ASP .Net (14) Azure (1) Breeze.js (1) C# (49) CloudComputing (1) CMS (1) CSS (2) Design_Pattern (3) DI (3) Dotnet (21) Entity Framework (3) ExpressJS (4) Html (3) IIS (1) Javascript (6) Jquery (9) Lamda (3) Linq (11) Mongodb (1) MVC (48) NodeJS (7) RDLC (1) Report (1) Sql Server (29) SSIS (3) SSRS (2) UI (1) WCF (12) Web Api (9) Web Service (1) XMl (1)