ASP.NET
  Home arrow ASP.NET arrow Page 3 - 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  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
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 / 19
    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 SqlDatabase class


    (Page 3 of 5 )

    The previous two sections concentrated on the database class to connect and work with Microsoft SQL Server.  In this section, I shall introduce you to connecting to a Microsoft SQL Server database using the SqlDatabase class.

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

    ImportsSystem.Data

    ImportsMicrosoft.Practices.EnterpriseLibrary.Data.Sql

    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 New SqlDatabase
    (ConfigurationManager.ConnectionStrings
    ("AdventureWorks").ConnectionString)

            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

    There exists not much difference between the code in the previous section and this section, except that I used the SqlDatabase class available in the “Microsoft.Practices.EnterpriseLibrary.Data.Sql” namespace.  You can also observe that I am using “ConfigurationManager” to retrieve connection string from the “web.config” file. 

    If you wanted to use the DatabaseFactory class to instantiate a SqlDatabase object, you can do it as follows:

    ImportsSystem.Data

    ImportsMicrosoft.Practices.EnterpriseLibrary.Data

    ImportsMicrosoft.Practices.EnterpriseLibrary.Data.Sql

    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 SqlDatabase = DirectCast
    (DatabaseFactory.CreateDatabase("AdventureWorks"), SqlDatabase)

            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

    More ASP.NET Articles
    More By Jagadish Chaterjee


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

    ASP.NET ARTICLES

    - More Advanced ASP.NET 3.5 Functions and Subr...
    - ASP.NET 3.5 Functions and Subroutines
    - Coding an IQ Test with Conditionally Driven ...
    - Developing Conditionally Driven Event Handle...
    - ASP.NET 3.5 Debugging Using Visual Web Devel...
    - Understanding Event Handlers in ASP.NET 3.5
    - Building a Web Form in ASP.NET and PHP: a Co...
    - Inserting Data into a Microsoft SQL 2008 Dat...
    - Creating an ASP.NET Dynamic Web Page Using M...
    - Retrieving Data from Microsoft SQL Server 20...
    - Building ASP.NET Web Forms to Use a MySQL Da...
    - Creating an ASP.NET Database using MS SQL 20...
    - Building an ASP.NET Website Using Include Ta...
    - Create ASP.NET Web Forms to Use a Microsoft ...
    - Editing Web Design Layout in Visual Web Deve...





    © 2003-2010 by Developer Shed. All rights reserved. DS Cluster 9 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek