Database Provider-Independent Data Access Layer Using ADO.NET 2.0

In .NET, the way of writing code for data access has changed. There no longer are different data provider Namespaces and Classes. For example, to connect Microsoft SQL Server 2000, Microsoft currently recommends that you use System.Data.SqlClient Namespace. This namespace has the following classes:

  • SqlConnection
  • SqlCommand
  • SqlDataAdapter
  • SqlDataReader
  • SqlParameter

The above classes take advantage of the better performance available through SQL Server's native Tabular Data Stream (TDS) interfaces. TDS is the protcol that MS SQL Server 2000 uses to connect between Client and DataSource.

Suppose you have written Data Acces Layer code based on the above SqlClient provider that only talks to MS SQL 2000 Server. Now, your application wants to connect to a different database—say, Oracle or MS SQL Server 6.5 (this is an OLE-DB Compliant DB). The DAL won't work with this new database. So, you need to change the Provider from System.Data.SqlClient to System.Data.OleDb provider and corresponding classes that support OleDb and rewrite the DAL code.

ADO.NET 2.0 will help you avoid this kind of problem, where you need not re-write DAL code when you change the data source. In ADO.NET 2.0, the Namespace System.Data.Common namespace has added some new classes that directly enable the implementation of provider-independent data access classes in the form of Factory Method Patterns.

Factory Patterns

A Factory pattern is one that returns an instance of one of several possible classes depending on the data provided to it. Or, you can say that, based on input, the Factory method creates the class object and returns. Usually, all of the classes it returns have a common parent class and common methods, but each of them performs a task differently. ADO.NET 2.0 provides a factory class to create Data access objects based on the provider that you pass.

In ADO.NET, the System.Data.Common namespace has the following base classes:

  • DbConnection
  • DbCommand
  • DbDataAdapter
  • DbParameter
  • DbTransaction
  • DbCommandBuilder
  • DbException

DbProviderFactory is the core class that you use to generate instances of the classes you need; for example, a connection, command, parameter, and so forth.

DbProviderFactories is a class that enables you to create specific instances of the DbProviderFactory class, each of which represents one of the managed code providers that are installed and available on the machine; for example, System.Data.SqlClient, System.Data.OleDb, ODBC, and so forth.

Here, I will use SqlClient as my Data Provider. I will write a Data Access code based on this SqlClient Provider.

#region Using directives

using System;
using System.Collections.Generic;
using System.Text;
using System.Data.Common ;


namespace DataProvider
   class Program
      static void Main(string[] args)
         string DataProviderName="System.Data.SqlClient";
         DbProviderFactory dpf =

         DbConnection dbConn= dpf.CreateConnection();
         dbConn.ConnectionString = "Database=AM;uid=XXXXX;pwd=XXXXX";

         DbCommand dbcmd = dpf.CreateCommand();
         dbcmd.CommandText = "Select * from  tDevelopers";
            dbcmd.Connection = dbConn;

         DbDataReader dr = dbcmd.ExecuteReader();

         while (dr.Read())
            Console.WriteLine("{0}, {1} , {2}",dr["Name"].ToString(),

Suppose I wanted to change my Data source to an OleDB-based Data provider. I just need to change string DataProviderName=" System.Data.OleDb";. This value also can be changed from the configuration.

For example, the configuration file entry for Data Provider will look like this:

Configuring a specific provider type

      <add key="provider-type" value="System.Data.SqlClient" />

You may want to use it if your application needs to be configurable to run against different database systems on a regular basis.

About the Author

SeenivasaRagavan Ramadurai

Seenivasaragavan Ramadurai is a .NET consultant, He has been working with .NET technology since pre beta releases. Seenivasa background includes Master's in Computer Science and B.Sc. Mathematics. He has over 9 years of software development experience with Microsoft technologies and has extensive experience developing client-server, distributed, Web services, and component based applications using Visual Studio .NET. Before moving to .NET, Seenivasa has worked on MFC, COM, ATL, and Visual C++ based applications. If you are looking for a consulting help, contact him at


  • about oracle

    Posted by wagnerp on 11/18/2009 05:16am

    what about the oracle that returns back a ref cursor ?

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • On-demand Event Event Date: October 29, 2014 It's well understood how critical version control is for code. However, its importance to DevOps isn't always recognized. The 2014 DevOps Survey of Practice shows that one of the key predictors of DevOps success is putting all production environment artifacts into version control. In this webcast, Gene Kim discusses these survey findings and shares woeful tales of artifact management gone wrong! Gene also shares examples of how high-performing DevOps …

  • On-demand Event Event Date: December 3, 2014 Microsoft SQL Server includes a high availability option called AlwaysOn Availability Groups (AGs). In this session we will see how AGs are implemented using a Windows Server Failover Cluster and SQL Server 2014 with the Pure Storage FlashArray. We will showcase how to use Pure Storage snapshots to seed secondary database copies and then replicate to another location for backup or disaster recovery operations. Windows PowerShell and the Pure Storage PowerShell …

Most Popular Programming Stories

More for Developers

RSS Feeds