How to write a .Net application that works with both SqlServer and Oracle (now that System.Data.OracleClient is deprecated)

前端 未结 13 736
礼貌的吻别
礼貌的吻别 2020-12-23 14:21

see also System.Data.OracleClient namespace discontinued?

(Firstly don’t panic yet, System.Data.OracleClient is not being removed by Microsoft yet, however it is not

13条回答
  •  执笔经年
    2020-12-23 14:53

    Use the "The Provider Factory Pattern" which is basically using a factory to give a data provider that is completely abstracted away from the database request methods that uses it here is a blogpost with some sample code that shows how to do this also Jean-Paul Boodhoo on Demystifying Design Patterns Part 1 on dnrtv.com shows how to as well.

    This is some very cool stuff basically you have a factory that provides a methods for getting a connection

    public IDbConnection GetConnection()
    {
         IDbConnection connection =  _frameworkDBProviderFactory.CreateConnection();
         connection.ConnectionString = _authenticationSettings.ConnectionString;            
         return connection;
    } 
    

    though an interface so you can call any type of Database who's connection object implements the IDbConnection interface (SQLServer, MySQL, Oracle, etc.) and it just works.

    By abstracting away what DB your using you can even swap them out at run time and your application will never know, it doesn't need to so to connection to an Orical DB, download the ODP.NET, same thing with mysql connector both implement IDbConnection, and write your code against the abstracted connection.

提交回复
热议问题