views:

132

answers:

2

Hi all,

We need the application we are building to be Database-agnostic. In particular, I need it to be able work with SQL Server, Oracle and MySQL. In the future, possibly other DB Vendors will be added to the list. Of course, whatever DB is to be used will have the same schema.

In order to build a prototype to demonstrate the interface, we loaded the schema in an SQL Server DB, and then generated the DataSet and TableAdapters for that database. As the dataset was generated by a specific instance of a database, I suspect that the generated DataSet and TableAdapters are not vendor independent. Furthermore, I am not even sure whether they can be used for another SQL Server DB instance.

My question is this: Is there a way to use the same auto-generated DataSet and TableAdapters with different databases (and possibly vendors)?

Cheers,

Markos

A: 

I think you are headed down the wrong path. I would go with NHibernate

I use it every day and it is database agnostic.

David Basarab
+2  A: 

In general, if you develop your database code against the System.Data.Common namespace, it will be as agnostic as it can be. There are plenty of caveats involved, however, since each database provider has its own particular implementation level details that differ from the rest.

An example of a database-agnostic approach using System.Data.Common looks like this:

var factory = DbProviderFactories.GetFactory("My Provider Name");
using (var connection = factory.CreateConnection())
{
    connection.ConnectionString = "My Connection String";
    connection.Open();

    var cmd = factory.CreateCommand();
    cmd.CommandText = "My Command Text";
    cmd.Connection = connection;

    var adapter = factory.CreateDataAdapter();
    adapter.SelectCommand = cmd;

    var dataset = new DataSet();
    adapter.Fill(dataset);

    // ...
}

Simply change the first line to switch database providers and the rest of code can pretty much remain the same.

This approach uses a lot of hand-coding. You might consider using a database framework like NHibernate (as mentioned by David Basarab), Subsonic or the Microsoft Enterprise Library Data Access Application Block to name a few.

Ray Vernagus