简体   繁体   中英

How to get only the provider connection string from Web.Config in EF setup?

I am working on a project which is using Entity Framework. However, I need to execute some SQL queries (without the use of EF), and would like to retrieve the connection string from the Web.Config. Getting the full connection string is fine using:

System.Configuration.ConfigurationManager.ConnectionStrings["DatabaseName"].ConnectionString;

Which returns the full connection string for the EF like this.

metadata=res://*/NameDB.csdl|res://*/NameDB.ssdl|res://*/NameDB.msl;provider=System.Data.SqlClient;provider connection string="Data Source=name-db;Initial Catalog=Name;Integrated Security=True;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient

I require just the "provider connection string" section from this. Is there any clever way of doing this? Or do I just have to search the string for it using something like regex?

Any help/advice appreciated. Thanks.

use the EntityConnectionStringBuilder Object to get the ProviderConnectionString

string s= System.Configuration.ConfigurationManager.ConnectionStrings["DatabaseName"].ConnectionString;
        System.Data.EntityClient.EntityConnectionStringBuilder e = new System.Data.EntityClient.EntityConnectionStringBuilder(s);
        string ProviderConnectionString = e.ProviderConnectionString;

Just use
var Provider = ConfigurationManager.ConnectionStrings["defautconnection"].ProviderName

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM