ASP.NET
  Home arrow ASP.NET arrow Page 3 - Database Independent Development using ASP...
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 
Dedicated Servers 
Moblin 
JMSL Numerical Library 
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

Database Independent Development using ASP.NET 2.0
By: Jagadish Chaterjee
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 20
    2007-02-14

    Table of Contents:
  • Database Independent Development using ASP.NET 2.0
  • Developing a common class which works with Factories: source code
  • Developing a common class which works with Factories: explanation
  • Developing a simple data access class to work with the factory: skeleton
  • Developing a simple data access class to work with the factory: methods
  • Developing a simple data access class to work with the factory: methods extended

  • 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


    Database Independent Development using ASP.NET 2.0 - Developing a common class which works with Factories: explanation


    (Page 3 of 6 )

    This section explains the source code provided in the previous section in a step-by-step manner. Let us start with the private members:

    Private _DBProviderName As String
    Private _ConnectionString As String

    Those two are the variables which hold the .NET data provider to use and the connection string to connect. For example, _DBProviderName can be assigned with "System.Data.SqlClient" (as a string itself) to work with Microsoft SQL Server specific .NET data provider. Similarly, you can even assign "Oracle.DataAccess.Client" to work with ODP.NET (or Oracle's .NET data provider to access Oracle).

    Going a bit further down, we have the following constructor:

    Public Sub New(ByVal ProviderName As String, ByVal ConnectionString As String)
     
    _DBProviderName = ProviderName
     
    _ConnectionString = ConnectionString
     
    If DbProviderFactories.GetFactoryClasses.Select _
       
    ("InvariantName='" & _DBProviderName & "'").Length = 0 Then
       
    Throw New Exception("Invalid .NET Data Provider specification: " & _DBProviderName)
       
    Exit Sub
     
    End If
     
    _dpf = DbProviderFactories.GetFactory(_DBProviderName)
    End Sub

    The above constructor receives ProviderName (ex: System.Data.SqlClient as a string) and ConnectionString as parameters, and checks whether or not the provider is installed on the machine. If the provider is not installed (say ODP.NET is not installed but you are trying to use Oracle.DataAccess.Client), then it raises an exception.

    If the .NET data provider is already installed, then it creates a factory object (based on the provider specification), which can be used later to create other objects like connections, commands etc. specific to that provider. Proceeding further down, we have the following:

    Public Function GetDBConnection() As DbConnection
     
    Dim dbConn As DbConnection = GetDBProviderFactory.CreateConnection
     
    dbConn.ConnectionString = _ConnectionString
     
    Return dbConn
    End Function

    The above function simply creates a Connection object (based on the .NET data provider selected by the factory), assigns the connection string and returns the same Connection object to the calling method. We can use this Connection object later to create other objects like Command, DataAdapter and so forth.

    Going further down, we have two more function which create and return Command/DataAdapter objects to the calling methods:

    Public Function GetDBCommand() As DbCommand
     
    Dim dbCmd As DbCommand = GetDBProviderFactory.CreateCommand
     
    dbCmd.Connection = GetDBConnection()
     
    Return dbCmd
    End Function

    Public Function GetDBDataAdapter() As DbDataAdapter
     
    Dim dbAdap As DbDataAdapter = GetDBProviderFactory.CreateDataAdapter
     
    Return dbAdap
    End Function

    More ASP.NET Articles
    More By Jagadish Chaterjee


       · Hello guys! This is new series on database independent development using ASP.NET...
     

    ASP.NET ARTICLES

    - 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
    - Developing a Dice Game Using ASP.NET Futures...
    - Completing an ASP.NET AJAX Server-Centric Ba...
    - Information Management for an ASP.NET AJAX S...
    - Comment and Order Management for an ASP.NET ...
    - Back-end Management Tasks for an ASP.NET AJA...
    - User Information Management for an ASP.NET A...
    - Adding Comments and Search to an ASP.NET AJA...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway