Enterprise Library: Your first step on DAAB
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.
- 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.
- Create a new project. I used a C# Windows application on my Visual Studio 2008 Express.
- Now you need to add reference to dlls
- Microsoft.Practices.EnterpriseLibrary.Common and
- Microsoft.Practices.EnterpriseLibrary.Data
- 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]; } } }
Related posts:























Leave a Reply
You must be logged in to post a comment.