views:

972

answers:

4

I need an example C# source code that abstracts databases (easy to change from one database to another with minimal code modification). Do you know some sample sites or tutorials with good quality?

Specific target databases are as follows:
1. Microsoft SQL Server
2. Oracle
3. MySQL
3. DB2

My specific requirements and encountered issues are the following:
1. Abstraction of classes used in data access.
2. Using parameters in calling stored procedures. In Microsoft SQL Server @ is fine. Other databases does not support @.
3. Converting query syntax from one database to another. Do we have some sort of "generic" query and then make some classes that generates queries it to a target database?
4. Strictly typed datasets in the data access layer. I remember from experience that the TableAdapter and Query wizard of Visual Studio failed for MySQL.

Thank you in advance for your expertise and time.

+4  A: 

Have a look at

and other ORMs

Mitch Wheat
+1 - An ORM like this will get you as far as you are likely to get without disproportionate effort. For the last handful of awkward queries you may have to write some custom code - merde happens. In these corner cases split the functionality into database dependent and database independent layers. Set up the database independent layers with a standard interface and a mechanism to plug in the database dependent modules.
ConcernedOfTunbridgeWells
if ORM then nHibernate, and stay away from Entity Framework!
Darius Kucinskas
@Darius Kucinskas: many people are using the Entity Framework successfully. Did you have specific issues with EF?
Mitch Wheat
@Mitch Wheat: One big issue the Entity Framework that it is tied to specific version of Net framework. Say if I started using EF1 which is tied to Net framework 3.5 sp1 and Visual Studio 2008 you just can't switch to EF4 which fixes many EF1 annoyances (one of them is lack of POCO support). You would have to switch to Net 4 framework and buy Visual Studio 2010. However I must admit Linq to Entities is nice feature.
Darius Kucinskas
A: 

Look at NHibernate site: https://www.hibernate.org/343.html

Lloyd
+1  A: 

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.

Aaron Daniels
+1  A: 

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.

moomi