Home DotNet Load data with Application Block of Enterprise Library 4.1
Author:

Enterprise library allows you to quick retrieves data from a relational database without you having to write all the tedious code. First you need to download the Enterprise Library http://msdn.microsoft.com/en-us/library/dd203099.aspx then install it onto your computer.

In your project reference the two DLLs:

  1. Microsoft.Practices.EnterpriseLibrary.Common
  2. Microsoft.Practices.EnterpriseLibrary.Data

Then in the Web.config file ensure you have the following tags.

    <configuration>

      <
    configSections>
        <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />        
           </configSections>
      <appSettings/>

      <
    connectionStrings>
        <add name="Production" connectionString="Database=AdventureWorks;Server=aupersqldev1;User Id=devuser; Password=devuser;" providerName="System.Data.SqlClient"/>
      </connectionStrings>

      <dataConfiguration defaultDatabase="Production"/>
      <system.web>

    Once you have setup the web.config file then you can start coding to retrieve data from the database. Here is the demo code. The code in LoadData method shows you how to retrieve data using Enterprise Library 4.1.

        1 using System;

        2 using System.Collections.Generic;

        3 using System.Linq;

        4 using System.Web;

        5 using System.Web.UI;

        6 using System.Web.UI.WebControls;

        7 using Microsoft.Practices.EnterpriseLibrary.Data;

        8 using Microsoft.Practices.EnterpriseLibrary.Data.Sql;

        9 using System.Data;

       10 using System.Data.SqlClient;

       11 using System.Data.Common;

       12 

       13 namespace RadGridWeb

       14 {

       15     public partial class WebForm1 : System.Web.UI.Page

       16     {

       17         protected void Page_Load(object sender, EventArgs e)

       18         {

       19             if (!Page.IsPostBack)

       20             {

       21                 LoadData();

       22             }

       23         }

       24 

       25         /// <summary>

       26         /// Load data into the Telerik RadGrid

       27         /// </summary>

       28         /// <returns></returns>

       29         private DataSet LoadData()

       30         {

       31             Database db = DatabaseFactory.CreateDatabase("Production");           

       32 

       33             string sqlCommand = "Select * From Purchasing.Vendor Where CreditRating = @CreditRating";

       34             DbCommand dbCommand = db.GetSqlStringCommand(sqlCommand);

       35 

       36             // Create and add integer input parameter

       37             db.AddInParameter(dbCommand, "CreditRating", DbType.Int32, 1);                              

       38 

       39             // DataSet that will hold the returned results

       40             DataSet dataSetVendor = null;

       41 

       42             // Open connection, execute the query and close connection

       43             dataSetVendor = db.ExecuteDataSet(dbCommand);

       44 

       45             return dataSetVendor;

       46         }

       47     }

       48 }



    Comments (0)
    Write comment
    Your Contact Details:
    Comment:
    Security
    Please input the anti-spam code that you can read in the image.

    "