How to use Database factory use key value as custom connection string

Topics: Data Access Application Block
Aug 30, 2013 at 3:24 AM
DatabaseProviderFactory factory = new DatabaseProviderFactory();
            Database db = factory.Create("ConString");
my boss do not use <add name="ConString" connectionString=

they use

<add key="DATABASE_SERVER" value=""/>
<add key="DATABASE_CATALOG" value="xmen"/>
<add key="DATABASE_LOGIN" value="sa"/>
<add key="DATABASE_PWD" value="123"/>
how to use database factory by passing connection string
Sep 2, 2013 at 5:52 AM
Edited Sep 2, 2013 at 5:53 AM
You will have to manually construct the connection string at runtime and set . Something like this should work:
    () => CreateDatabaseFromAppSettings(),
    name => CreateDatabaseFromAppSettings());

var db = DatabaseFactory.CreateDatabase();

private Database CreateDatabaseFromAppSettings()
    return new SqlDatabase(GetConnectionString());

private string GetConnectionString()
    DbConnectionStringBuilder connectionStringBuilder = new SqlConnectionStringBuilder();
    connectionStringBuilder.Add("Server", ConfigurationManager.AppSettings["DATABASE_SERVER"]);
    connectionStringBuilder.Add("Database", ConfigurationManager.AppSettings["DATABASE_CATALOG"]);
    connectionStringBuilder.Add("User Id", ConfigurationManager.AppSettings["DATABASE_LOGIN"]);
    connectionStringBuilder.Add("Password", ConfigurationManager.AppSettings["DATABASE_PWD"]);

    return connectionStringBuilder.ConnectionString;

Randy Levy
Enterprise Library support engineer
Support How-to