C# Database abstraction for Microsoft SQL Server, Oracle, MySQL and DB2 C# Database abstraction for Microsoft SQL Server, Oracle, MySQL and DB2 oracle oracle

C# Database abstraction for Microsoft SQL Server, Oracle, MySQL and DB2


Have a look at

and other ORMs


Although I highly recommend NHibernate, you may also want to look at the Data Access application block of Microsoft's Enterprise Library.

Of course, any ORM should provide the functionality you need.


The best way to approach this is to use the interfaces provided by the database providers; they are nearly parallel in functionality. Create a static factory class to create interfaces for Command adapters, data adapters, and connections, based on the database which is configured. For instance:

    public static IDbDataAdapter GetDataAdapter (Database db)    {        switch (db)        {            default:            case "MsSql":                return new SqlDataAdapter ();            case "MySql"                return new MySqlDataAdapter ();        }    }    public static IDbCommand GetCommand (Database db)    {        switch (db)        {            default:            case "MsSql":                return new SqlCommand ();            case "MySql"                return new MySqlCommand ();        }    }

Your client code won't know the difference, though it will have to pass the configuration string around. Use VS docs to examine the interfaces given by each of the objects you normally use, and stick to using those, and it will be pretty straightforward - though you may have to hack your way through a couple of things.