ASP.NET
  Home arrow ASP.NET arrow Page 4 - 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 GenericDatabase class


    (Page 4 of 5 )

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

    The following code uses the GenericDatabase 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 New GenericDatabase
    (ConfigurationManager.ConnectionStrings
    ("AdventureWorks").ConnectionString,
    System.Data.SqlClient.SqlClientFactory.Instance)

            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 is not much difference between the code in the previous sections and this section, except that I used the GenericDatabase class available in the “Microsoft.Practices.EnterpriseLibrary.Data” namespace. You can also observe that I am using “ConfigurationManager” to retrieve connection string from the “web.config” file. 

    Connecting to OLEDB data sources using the GenericDatabase class

    The previous section concentrated on using the GenericDatabase class to connect and work with Microsoft SQL Server.  In this section, I shall continue with the same class and work with OLEDB data sources.

    Before you begin, modify your web.config as follows:

    <configuration>

          <appSettings/>

          <connectionStrings>

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

                <add name="AdventureWorks" connectionString="Provider=
    sqloledb;Data Source=.sql2k5;Initial
    Catalog=AdventureWorks;Integrated Security=SSPI;
    "/>

          </connectionStrings>

    The following code uses the GenericDatabase class to connect to Microsoft SQL Server database using an OLEDB connection string.

    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 New GenericDatabase
    (ConfigurationManager.ConnectionStrings
    ("AdventureWorks").ConnectionString,
    System.Data.OleDb.OleDbFactory.Instance)

            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

    - 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