This sample code snippet gives you a kick start (Hello World ;)) on Microsoft Patterns & Practices Enterprise Library – Data Access Application Block.

Remember that there a more than one way to do most steps. I am not going to confuse you by writing all.

 

  1. First you need to download and install Enterprise Library. I used version 4.1 which is the latest at the time of writing this entry.
  2. Create a new project. I used a C# Windows application on my Visual Studio 2008 Express.
  3. Now you need to add reference to dlls
    • Microsoft.Practices.EnterpriseLibrary.Common and
    • Microsoft.Practices.EnterpriseLibrary.Data
  4. Below code does the following:
    • Create database connection. I used a SQL Server 2008 Database.
    • Execute a SELECT statement
    • Populates data to a DataGridView control
using System;
using System.Windows.Forms;
 
using System.Data;
using System.Data.Common;
 
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
 
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            string strCon = @"Database=test;Server=localhost;
					Integrated Security=SSPI";
 
            SqlDatabase db = new SqlDatabase(strCon);
            DbCommand cmd = db.GetSqlStringCommand("SELECT *FROM test");
 
            DataSet ds = db.ExecuteDataSet(cmd);
 
            dataGridView1.DataSource = ds.Tables[0];
        }
    }
}
VN:F [1.1.6_502]
Rating: 5.0/5 (1 vote cast)
Share:
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • DotNetKicks
  • LinkedIn
  • Live
  • MySpace
  • StumbleUpon
  • Technorati

Related posts:

  1. Enterprise Library 4.0 for Visual Studio 2008 Released
  2. Your first SQLite program on .NET
  3. Extreme beginner’s LINQ sample in VB.NET
  4. Goals of Enterprise Library
  5. WinForms – Print using printDialog and printDocument