1
Vote

Specify connection name in config file

description

Allow the developer to specify an existing connection string name that may exist in the config file instead of the connection string. This will allow the developer to have only one connection string in his database and make things easier when the connection string changes, because then he doesn't need to change the connection string in more than one place.
 
This can be achieved by doing the following:
private const string CONNECTION_NAME = "ConnectionName";
 
Change the initialize method as follows (in the provider):
public override void Initialize(string name, NameValueCollection config)
    {
        base.Initialize(name, config);
        connectionString_ = config[CONNECTION_STRING_KEY];
        if (string.IsNullOrEmpty(connectionString_))
        {
            string connectionName = config[CONNECTION_NAME];
            if (!string.IsNullOrEmpty(connectionName))
            {
                connectionString_ = System.Configuration.ConfigurationManager.ConnectionStrings[connectionName].ConnectionString;
            }
            else
            {
                throw new InvalidOperationException("There should be atleast a ConnectionName or ConnectionString be specified.");
            }
        }
 
        var install = config[INSTALL_KEY];
        bool doInstall;
        if (Boolean.TryParse(install, out doInstall)
            && doInstall)
        {
            var installConnectionString = config[INSTALL_CONNECTION_STRING_KEY];
            Install(installConnectionString, connectionString_);
        }

        Connection.Open();
}

comments