Open Microsoft.practices.EnterpriseLibrary database with just a connection string

18,501

Solution 1

You can also do


Database mydb = new EnterpriseLibrary.Data.Sql.SqlDatabase("connection string here");

and you'll retain the versatility of the Database object, but create it with a connection string.

Solution 2

I am building a connection string on the fly dynamically with the following where ConnectionString is in my .config

     Database configdb = DatabaseFactory.CreateDatabase("ConnectionString");

                DbConnectionStringBuilder sb = configdb.DbProviderFactory.CreateConnectionStringBuilder();

                // Set the Connection String fields.

                sb["Data Source"] = targetServer; 
                sb["Initial Catalog"] = targetDb; 
                sb["User ID"] = username;
                sb["Password"] = password;
                sb["Connect Timeout"] = dbTimeout;

                GenericDatabase newDb = new GenericDatabase(sb.ToString(), configdb.DbProviderFactory);
Database db = newDb;
Share:
18,501
Admin
Author by

Admin

Updated on June 05, 2022

Comments

  • Admin
    Admin almost 2 years

    I am using the Microsoft.Practices.EnterpriseLibrary Database tools and I'm having trouble creating a new database using just the connection string information.

    Ideally I would like to do the following:

    Database dbEngine = DatabaseFactory.CreateDatabase(
        "Data Source=myServerAddress;Initial Catalog=myDataBase;Integrated Security=SSPI;");
    

    Is it possible to create a database using just the connection string?

    If so, how can it be achieved?