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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With