Agnostic Connection handlers in .NET.

I am developing a simple database asinter for the system, but I need to support 2 databases, PostgreSQL and Oracle (XXg);

An example would be like this (already works for pgsql)

... bunch of properties, constructors and fields

private String BuildConnectionString();

public Int32 ExecuteNonQuery(NpgsqlCommand sqlCommand);

public NpgsqlDataReader ExecuteQueryReader(NpgsqlCommand sqlCommand);

public Object ExecuteScalar(NpgsqlCommand sqlCommand);

I was wondering if it is possible to build a class to handle these two databases using only System.Data interfaces such as IDataReader, IDbConnection, etc. I think it is, but I got a little stuck when I needed to specify a connection. Example:

    public PgSqlConnectionHandler(String username, String password, 
               String database, String server, Int32 port)
    {
        this._username = username;
        this._password = password;
        this._database = database;

        this._server = server;
        this._port = port;

        this._connection = new NpgsqlConnection(BuildConnectionString());
    }

Otherwise, I would have (_connect typeof (IDbConnection):

public AgnosticConnectionHandler(String userName, String password, 
               String database, String server, Int32 port)
    {
        this._userName = userName;
        this._password = password;
        this._database = database;
        this._server = server;
        this._port = port;

        this._connection = ????
    }

Is there a quick way to do this?

These are simple database queries, I don't need anything fancy, this is a small system.

Thank you for the attention!

+3
3

Factory .

I.e., , , , , (IDbConnection) ( ).

, , if, Oracle Postgre.

+2

IDbConnection factory . IDbCommands DataReaders DataSets. , .

+2

Source: https://habr.com/ru/post/1727478/


All Articles