views:

1962

answers:

4

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?

A: 

Er... That's because CreateDatabase expects the name of a connection string (as specified in your app's config file), not the connection string itself.

Paul Lalonde
That was just an example of what I was attempting to do, I wondered if it were somehow possible to do that using another method.
Scott Vercuski
A: 

I discovered that you can use the following command ...

SqlDatabase dbEngine = new SqlDatabase(connectionString);

not quite as versatile as the Database object but it works for my purpose.

Thanks everyone!

Scott Vercuski
+2  A: 

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;
kiev
+3  A: 

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.

pete the pagan-gerbil