ASP.NET
  Home arrow ASP.NET arrow Page 2 - Connecting to Different Databases Using AS...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP.NET

Connecting to Different Databases Using ASP.NET 2.0
By: Jagadish Chaterjee
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 18
    2006-10-09

    Table of Contents:
  • Connecting to Different Databases Using ASP.NET 2.0
  • Connecting to Microsoft SQL Server database using the database class: source code
  • Connecting to Microsoft SQL Server database using the SqlDatabase class
  • Connecting to Microsoft SQL Server database using the GenericDatabase class
  • Connecting to an Oracle database using the GenericDatabase class

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Connecting to Different Databases Using ASP.NET 2.0 - Connecting to Microsoft SQL Server database using the database class: source code


    (Page 2 of 5 )

    The previous section explained in detail every database class existing in “Microsoft Enterprise Library 2.0.” In this section, I shall introduce you to connecting to Microsoft SQL Server database using the database class.

    To start with, modify your web.config class to look something like the following:

    <configuration>

          <appSettings/>

          <connectionStrings>

                <add name="AdventureWorks" connectionString=
    "Database=AdventureWorks;Server=(local)SQL2k5;Integrated Security=SSPI;" providerName="System.Data.SqlClient"/>

          </connectionStrings>

          <system.web>

    The following code uses the database class to connect to a Microsoft SQL Server database.

    ImportsSystem.Data

    ImportsMicrosoft.Practices.EnterpriseLibrary.Data

    PartialClass _Default

        Inherits System.Web.UI.Page

        Protected Sub btnConnect_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnConnect.Click

            Dim db As Database

            db = DatabaseFactory.CreateDatabase("AdventureWorks")

            Me.lblMsg.Text = db.ConnectionStringWithoutCredentials

            Dim dt As DataTable = db.ExecuteDataSet(CommandType.Text, "select * from HumanResources.department").Tables(0)

            Me.GridView1.DataSource = dt

            Me.GridView1.DataBind()

        End Sub

    EndClass

    The next section will explain the above code.

    Explaining the source code

    This section explains the code listed in the previous section.  Let us go part by part.  Consider the following:

    ImportsSystem.Data

    ImportsMicrosoft.Practices.EnterpriseLibrary.Data

    To work with datatable or dataset objects, we need to import the “System.Data” namespace.  To deal with the database class (of Enterprise Library), we need to import the “Microsoft.Practices.EnterpriseLibrary.Data” namespace.

    Further proceeding we have the following:

            Dim db As Database

            db = DatabaseFactory.CreateDatabase("AdventureWorks")

    You can observe that I am using the DatabaseFactory class to instantiate a database object.  We cannot directly create an object of class database using the “new” operator as it is an abstract class. The DatabaseFactory class has a “static” (or “shared”) method named “CreateDatabase,” which is mainly used to create database objects.

    Once a database object is instantiated, we can get or view the connection string information using the following statement:

            Me.lblMsg.Text = db.ConnectionStringWithoutCredentials

    Further proceeding, we have the following:

            Dim dt As DataTable = db.ExecuteDataSet(CommandType.Text, "select * from HumanResources.department").Tables(0)

    The above executes a “SELECT” statement and finally returns a data table, which contains all the rows retrieved by that statement.  Finally, I show all those rows on a “GridView” using the following two statements:

            Me.GridView1.DataSource = dt

            Me.GridView1.DataBind()

    That ends the explanation for the code given in the previous section.

    More ASP.NET Articles
    More By Jagadish Chaterjee


       · Hello guys. This is second in the series on working with Microsoft Enterprise...
     

    ASP.NET ARTICLES

    - Disadvantages of the ASP.NET MVC Framework
    - Advantages of the ASP.NET MVC Approach
    - ASP.NET Web Forms Weaknesses
    - ASP.NET Web Forms Meets ASP.NET MVC
    - Source Code for Saving and Retrieving Data w...
    - Using GridView to Save and Retrieve Data wit...
    - Handling Dynamic Images in ASP.NET 3.5 AJAX ...
    - Retrieving Data with AJAX and the GridView C...
    - Playing with Images in ASP.NET 3.5 AJAX Appl...
    - Saving and Retrieving Data with AJAX
    - Enhancing PHP Via the ASP.NET AJAX Framework...
    - Enhancing PHP Programming with the ASP.NET A...
    - Classes and ASP.NET AJAX
    - Using ASP.NET AJAX
    - Building a Simple Storefront with LINQ





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT