0

I'm trying to find a generic way for multiple db connection support.

        private void ExecuteCommand(string connStr, Action<NpgsqlConnection> task)
    {

            using (var conn = new NpgsqlConnection(connStr))
            {
                conn.Open();

                task(conn);
            }
      }

I have 2 Databases which are sql and postgresql. I dont want to duplicate my function 2 of them. So I need something like that:

if (db == sql) new sqlConnection() else new NpsqlConnection()

How can i do that in simple way?

1 Answer 1

1

There are a common set of base classes and interfaces for ADO.NET. EG

 private void ExecuteCommand(string sql, DbConnection con)
 {
    var cmd = con.CreateCommand();
    cmd.CommandText = sql;
    cmd.ExecuteNonQuery();
 }
Sign up to request clarification or add additional context in comments.

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.