Microsoft SQL Server、Oracle、MySQL 和 DB2 的 C# 数据库抽象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/916425/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 18:17:56  来源:igfitidea点击:

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

c#mysqloraclesql-server-2005

提问by yoitsfrancis

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?

我需要一个抽象数据库的示例 C# 源代码(只需修改最少的代码即可轻松地从一个数据库更改为另一个数据库)。你知道一些质量好的示例站点或教程吗?

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

具体目标数据库如下:
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.

我的具体要求和遇到的问题如下:
1. 数据访问中使用的类的抽象。
2. 在调用存储过程时使用参数。在 Microsoft SQL Server @ 中很好。其他数据库不支持@。
3. 将查询语法从一种数据库转换为另一种数据库。我们是否有某种“通用”查询,然后创建一些类来生成对目标数据库的查询?
4. 数据访问层严格类型的数据集。我记得根据经验,Visual Studio 的 TableAdapter 和查询向导对 MySQL 失败了。

Thank you in advance for your expertise and time.

预先感谢您的专业知识和时间。

回答by Mitch Wheat

Have a look at

看一下

and other ORMs

和其他 ORM

回答by Aaron Daniels

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

虽然我强烈推荐NHibernate,但您可能还想查看Microsoft 企业库的数据访问应用程序块。

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

当然,任何 ORM 都应该提供您需要的功能。

回答by moomi

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.

您的客户端代码不会知道区别,但它必须传递配置字符串。使用 VS 文档检查您通常使用的每个对象提供的接口,并坚持使用它们,这将非常简单 - 尽管您可能需要通过一些方法来破解。