Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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;

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.

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!

It's solved my problem. I have a single web app using many database, according to the different subdomain in the url to connect to the different database. such as:


  • abc.test.com ------>using the Db_projectABC

  • def.test.com ------>using the db_ProjectDEF


I use url-rewrite to parse the subdomain name, and use the subdomain name to chose it's database connection string which stored in the main database.

Thanks

Database objDatabase = new Microsoft.Practices.
                    EnterpriseLibrary.Data.Sql.SqlDatabase(@"Password=mypassword;Persist Security Info=True;User ID=sa;Initial Catalog=DatabaseName;Data Source=192.168.2.1"); 
      // Data Sourc is Your DB Name for ex : systemIp\\SqlExpress

            DbConnection objCon = objDatabase.CreateConnection();
            objCon.Open();
            objDbTransaction = objCon.BeginTransaction(IsolationLevel.ReadUncommitted);
            DbCommand cmd = objDatabase.GetSqlStringCommand(" Select sName from Emp");

            SqlDataReader dr = (SqlDataReader)objDatabase.ExecuteReader(cmd, objDbTransaction);

            StringBuilder sb = new StringBuilder();

            while (dr.Read())
            {
                sb.Append(Convert.ToString(dr[0]));
                sb.Append("  ");
            }
            objDbTransaction.Commit();
            objCon.Close();

            //objDatabase.DbProviderFactory.
            return sb.ToString();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top