C # - List of Database Connections Strings Provider

I am trying to develop a dotnet application with multiple database providers and I need to know the ConnectionString and Provider from the most used databases. I am using System.DBCommon. This is my code:

public  class DBConnector
{

  public void ConectDatabase()
  {
      {
          string connectionString =
            "Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)" +
            "(HOST=MYHOST)(PORT=1527))(CONNECT_DATA=(SID=MYSERVICE)));" +
            "User Id=MYUSER;Password=MYPASS;"; //Connection String
          string provider =
            "Oracle.DataAccess.Client.OracleConnection, Oracle.DataAccess"; //I need this  for the most used databases (Mysql, PostgreSQL, SqlServer)


          using (DbConnection conn = (DbConnection)Activator.
            CreateInstance(Type.GetType(provider), connectionString))
          {
              conn.Open();
              string sql =
                "select distinct owner from sys.all_objects order by owner";
              using (DbCommand comm = conn.CreateCommand())
              {
                  comm.CommandText = sql;
                  using (DbDataReader rdr = comm.ExecuteReader())
                  {
                      while (rdr.Read())
                      {
                          string owner = rdr.GetString(0);
                          Console.WriteLine("{0}", owner);
                      }
                  }
              }
          }
      }
  }

      

I found links on this site https://www.connectionstrings.com/

But I also need a provider. Thanks to

+3


source to share


2 answers


The provider name in the connection string attribute is not a class but a namespace for example. System.Data.SqlClient is not a class but a namespace, in that namespace you have SqlConnection, SqlCommand, etc.

You can try to find all the classes that implement the interface IDbConnection

and then create an IDbConnection based on that type:

var types = AppDomain.CurrentDomain.GetAssemblies()
    .SelectMany(s => s.GetTypes())
    .Where(p => typeof(IDbConnection).IsAssignableFrom(p) && p.IsClass);

foreach(var dbConnection in types)
{
    Console.WriteLine(dbConnection);
}

      



After installing the MySQL and Oracle packages, this was the resulting list

  • System.Data.SqlClient.SqlConnection
  • System.Data.OleDb.OleDbConnection
  • System.Data.Odbc.OdbcConnection
  • System.Data.Common.DbConnection
  • MySql.Data.MySqlClient.MySqlConnection
  • Oracle.ManagedDataAccess.Client.OracleConnection

You can check the source code here https://github.com/kblok/StackOverflowExamples/blob/master/AspNetDemoProject/AspNetDemoProject/Demos/ProvidersList.aspx.cs

+2


source


You can list the registered db providers like this:

using System;
using System.Data;
using System.Data.Common;

namespace StackOverflowExamples
{
    class AvailableDataProviders
    {
        public static void Main(string[] args)
        {
            using (DataTable providers = DbProviderFactories.GetFactoryClasses())
            {
                Console.WriteLine("Available Data Providers:");

                foreach (DataRow provider in providers.Rows)
                {
                    Console.WriteLine();
                    Console.WriteLine("Name: {0}", provider["Name"]);
                    Console.WriteLine("Description: {0}", provider["Description"]);
                    Console.WriteLine("Invariant Name: {0}", provider["InvariantName"]);
                    Console.WriteLine("AssemblyQualifiedName: {0}", provider["AssemblyQualifiedName"]);
                }
            }
        }
    }
}

      



Your app assemblies may have provider classes that are not registered with your application (in app.config or web.config), or on your machine (in machine.config), or within a framework.

Your app can only be used by providers registered with system.data -> DbProviderFactories

your app.

0


source







All Articles