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

梦想的初衷 提交于 2019-12-18 09:12:29

问题


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?


回答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.




回答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;



回答3:


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.




回答4:


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!




回答5:


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




回答6:


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();


来源:https://stackoverflow.com/questions/401339/open-microsoft-practices-enterpriselibrary-database-with-just-a-connection-strin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!