Skip Navigation Links
Home
About
Blog
Resume
Calendar Demo
Map Demo
Birthday Odds
Mozy

I read this article  on MSDN about doing generic database access in .NET 2.0 and decided to replace what I was doing with the Microsoft Enterprise Library Data Access Block with generic access.

For this integration, I used the following libraries:

  • fbembed.dll (Version 1.5.2.4731)
  • firebird.msg (it comes with fbembed.dll)
  • FirebirdSql.Data.FirebirdClient.dll (Version 2.0.0.0 from the Firebird .NET Provider for .NET 2.0 Version Alpha 2)

The point in .NET 2.0 seems to be that all database access code can be generically accessed through an object that inherits DbProviderFactory and that you can instantiate that object from the DbProviderFactories object.

There is some initial configuration involved.  The object that intherits DbProviderFactory needs to be defined in the .config file for the application.  It makes it kinda nice to have the connectionstring defined in the .config file too.

I did my example code with a class that I just stuck in the App_Code directory of an ASP.NET application.  Here is the class:

using System;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.IO;
using System.Runtime.InteropServices;


namespace BALConsultingNet.Data
{

/*
Make sure that the following files are in the Bin directory
fbembed.dll
firebird.msg
FirebirdSql.Data.FirebirdClient.dll

Add this to Web.Config to define the Firebird Data Provider
<system.data>
<DbProviderFactories>
<add name="Firebird Data Provider" invariant="FirebirdSql.Data.FirebirdClient" description=".Net Framework Data Provider for Firbird" type="FirebirdSql.Data.FirebirdClient.FirebirdClientFactory, FirebirdSql.Data.FirebirdClient" />
</DbProviderFactories>
</system.data>

Add this to Web.Config to define the connection string
<connectionStrings>
<add
name="FirebirdConnection"
connectionString="database=|DataDirectory|FirebirdEmbed.fdb;Integrated Security=True;server=server;Password=PassDefault;ServerType=1;UserID=UserDefault"
providerName="FirebirdSql.Data.FirebirdClient" />
</connectionStrings>

*/


public class DataGeneric
{
   private string _sConnectionString;
   private string _sProviderName;
   private DbProviderFactory _oDataFactory;

   public DataGeneric(string lsConnectionStringName, string lsDataDirectory)
   {
      _sConnectionString = ConfigurationManager.ConnectionStrings
                           [lsConnectionStringName].ConnectionString.Replace(
                           
"|DataDirectory|",
                           lsDataDirectory);
      _sProviderName = ConfigurationManager.ConnectionStrings[lsConnectionStringName].ProviderName;
      _oDataFactory = DbProviderFactories.GetFactory(_sProviderName);

   }
}

Instantiating the object:

DataGeneric loData = new DataGeneric("FirebirdConnection", Server.MapPath("~/App_Data/"));

gives the object access to a _oDataFactory object that can be used to create generic database objects like DbCommand, DbCommandBuilder, DbConnection, DbConnectionStringBuilder, DbDataAdapter, and DbParameter.

Categories: Development | Database
Posted by Brian Lakstins on Sunday, November 13, 2005 7:00 PM
Permalink | Comments (0) | Post RSSRSS comment feed
Comments are closed