C# 用于多个数据库的ADO.NET或OLE?

C# 用于多个数据库的ADO.NET或OLE?,c#,.net,ado.net,C#,.net,Ado.net,我的任务是编写一个类,用于连接到多个不同的数据库,如Oracle、SQL Server、Spatialite等,并获取和设置数据值。我想知道您会推荐标题中的哪一个 我创建了一个类,它利用OLE功能来建立连接、设置和检索数据,因为我认为ADO更像是一种SQL Server特有的技术。是这种情况还是ADO.NET可以用于各种数据源?我已经对它进行了很多研究,我所看到的大多数地方都建议对多个可能的数据源使用OLE,而对SQL Server则建议使用ADO 谢谢,ADO.NET可用于通过ODBC连接连接

我的任务是编写一个类,用于连接到多个不同的数据库,如Oracle、SQL Server、Spatialite等,并获取和设置数据值。我想知道您会推荐标题中的哪一个

我创建了一个类,它利用OLE功能来建立连接、设置和检索数据,因为我认为ADO更像是一种SQL Server特有的技术。是这种情况还是ADO.NET可以用于各种数据源?我已经对它进行了很多研究,我所看到的大多数地方都建议对多个可能的数据源使用OLE,而对SQL Server则建议使用ADO


谢谢,ADO.NET可用于通过ODBC连接连接到任何数据库。我们使用ADO.NET处理几乎所有的事情(PostgreSQL、Oracle、MySQL、SQL Server等),它是大多数ORM产品的核心数据库访问方法。

我建议您通过一个接口抽象出DAL(数据访问层),然后为每种类型的数据库创建多个DAL

如果对DALs私有代码使用基于提供程序的ORM(如实体框架),您的任务会变得容易得多


对于公共部分,您所要做的就是实现接口契约,您很好。

我看不出OLE将如何为统一DAL提供灵丹妙药。您希望使用最适合数据源的数据访问框架,并将实现从应用程序中抽象出来

我的猜测是,您希望使用OLE将允许语法的某种统一。但事实并非如此。一旦您尝试执行诸如分页之类的操作,每个数据源都会有自己的怪癖,并且您如何连接到源代码将无助于统一语法差异。例如,在分页的情况下,SQLite使用SKIP,TAKE。Oracle使用ROWNUM。SQLServer使用TOP

我将通过创建一个DAL来解决这个问题,该DAL将实体类从数据源中抽象出来。然后可以使用最适合数据源的连接类型。例如,假设我的应用程序想要从数据源检索“产品”。我的应用程序只知道ProductFactory接口,而不知道数据源类型的细节。比如说,

string id = "0000001";
IProductFactory factory = FactoryManager.GetProductFactory();
Product p = factory.Get(id);
在下面的示例中,我使用ODP.Net(Oracle数据源的最佳数据访问组件)为产品接口实现了Oracle数据访问层。然后,数据访问层如下所示:

i产品工厂:

public interface IProductFactory: IEntityFactory
{
    Product Get(string productId);
    IList<Product> GetAll();
    int GetAllCount();
}
public class Product : IEntity<Product>
{
    public string ProductId { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }

    #region IEntity<Product> Members

    public Product MapData(System.Data.IDataReader reader)
    {
        return new Product
        {
            Name = reader["name"] as string,
            Description = reader["description"] as string
        };
    }

    #endregion
}
公共接口IPProductFactory:IEntityFactory
{
产品获取(字符串productId);
IList GetAll();
int GetAllCount();
}
产品:

public interface IProductFactory: IEntityFactory
{
    Product Get(string productId);
    IList<Product> GetAll();
    int GetAllCount();
}
public class Product : IEntity<Product>
{
    public string ProductId { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }

    #region IEntity<Product> Members

    public Product MapData(System.Data.IDataReader reader)
    {
        return new Product
        {
            Name = reader["name"] as string,
            Description = reader["description"] as string
        };
    }

    #endregion
}
公共类产品:智能
{
公共字符串ProductId{get;set;}
公共字符串名称{get;set;}
公共字符串说明{get;set;}
#地区成员
公共产品地图数据(System.Data.IDataReader阅读器)
{
退回新产品
{
Name=读卡器[“Name”]作为字符串,
Description=读卡器[“Description”]作为字符串
};
}
#端区
}
Oracle ProductFactory

public class ProductFactory : EntityFactory, IProductFactory
{
    #region IProductFactory Members

    public Product Get(string productId)
    {
        Product p = null;
        using (OracleConnection conn = new OracleConnection(ConnectionString))
        {
            using (OracleCommand cmd = new OracleCommand())
            {
                cmd.CommandText = "select * from product where productId = :productId";
                cmd.Parameters.Add("productId", productId);
                using (IDataReader reader = cmd.ExecuteReader())
                {
                    p = new Product();
                    p.MapData(reader);
                }
            }
        }
        return p;
    }

    public IList<Product> GetAll()
    {
        throw new NotImplementedException();
    }


    public int GetAllCount()
    {
        throw new NotImplementedException();
    }

    #endregion
}
公共类ProductFactory:EntityFactory、IPProductFactory
{
#地区工厂成员
公共产品Get(字符串productId)
{
乘积p=null;
使用(OracleConnection conn=新的OracleConnection(ConnectionString))
{
使用(OracleCommand cmd=new OracleCommand())
{
cmd.CommandText=“从产品中选择*,其中productId=:productId”;
cmd.Parameters.Add(“productId”,productId);
使用(IDataReader=cmd.ExecuteReader())
{
p=新产品();
p、 地图数据(读取器);
}
}
}
返回p;
}
公共IList GetAll()
{
抛出新的NotImplementedException();
}
public int GetAllCount()
{
抛出新的NotImplementedException();
}
#端区
}

我几乎把你投了一票——理论上的论证很好,20年来过时的建厂方法完全忽略了自.NET 3.5和LINQ以来的一切新东西。当然,我们可以在实体框架LINQ中替代数据访问层。天空是极限。在Oracle的例子中,本机ODP.Net实体框架仍处于测试阶段,并且存在一些问题。有时你所需要的只是一个“产品”。很难知道OP的数据源有多复杂。不希望这样统一语法。我有一个WCF服务,我在其中编写了一个名为oledbconnection.cs的类。此类处理与数据库的连接,并且还内置了所有函数来运行纯SQL并返回结果。我让它在oracle数据库上工作,唯一需要更改的是连接字符串。不同的数据库对某些基本SQL函数(如SELECT等)使用不同的词是否很常见??该类将仅用于纯SQL,因此不会调用存储过程等。听起来DAL将隐藏在您的WCF服务后面。我真的觉得你用OLEDB连接类找错了方向。您不希望数据源绑定到OLEdb。您希望有一个实现特定接口的数据访问层。它如何连接到数据库的影响较小。实体框架或NHibernate之类的ORM将引导您走上这条道路。也许我的手工编码来源是个坏主意。但我认为,他们已经明白了这一点。是的,根据我的经验,SQL语句会因源而异。正如我所说,寻呼就是一个例子。@Brett感谢您提供的所有信息。我会考虑一下你的建议。你说的话确实很像我需要的。所以你推荐实体框架或NHi