5

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.

3 Answers 3

7

Have a look at

and other ORMs

Sign up to request clarification or add additional context in comments.

3 Comments

+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.
@Darius Kucinskas: many people are using the Entity Framework successfully. Did you have specific issues with EF?
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.
3

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.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.