How to recognize providerName from app.config

May 21, 2009 at 1:35 PM

My application shoul work with two different databases: Oracle and SQL Server. So, in the app.config file I've only one configuration string

 

 <connectionStrings>

    <add name="CoreStorage" connectionString="Data Source=(local);Initial Catalog=CoreStorage;User ID={0};Password={1}"

      providerName="System.Data.SqlClient" />

  </connectionStrings>

or

 <connectionStrings>

    <add name="CoreStorage" connectionString="Data Source=(local);Initial Catalog=CoreStorage;User ID={0};Password={1}"

      providerName="System.Data.OracleClient" />

  </connectionStrings>

So, when an application gets a user login/password I should put them instead of {0} and {1} and create the databse  according to a provider name.

But I can't understand how to do it easy. My way is

 

public static class DatabaseFactory

    {

        public static Microsoft.Practices.EnterpriseLibrary.Data.Database CreateDatabase(string login, string password)

        {

            Microsoft.Practices.EnterpriseLibrary.Data.Database result = null;

            var db = Microsoft.Practices.EnterpriseLibrary.Data.DatabaseFactory.CreateDatabase();

            if (userAccess == null)

            {

                result = db;

            }

            else

            {

                //Data Source=(local);Initial Catalog=CoreStorage;User ID={0};Password={1}

                //Data Source=Nicotest;User ID={0};Password={1);Unicode=True

                string connection_string = string.Format(db.ConnectionString, login, password);


                if (db is Microsoft.Practices.EnterpriseLibrary.Data.Sql.SqlDatabase)

                {

                    result = new Microsoft.Practices.EnterpriseLibrary.Data.Sql.SqlDatabase(connection_string);

                }

                else if (db is Microsoft.Practices.EnterpriseLibrary.Data.Oracle.OracleDatabase)

                {

                    result = new Microsoft.Practices.EnterpriseLibrary.Data.Oracle.OracleDatabase(connection_string);

                }

            }

            return result;

        }

    }

 

I believe there must be another way. Could somebody give any an advise how to improve it.