Reputation: 3071
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.
Upvotes: 6
Views: 5167
Reputation: 229
Just use
var Provider = ConfigurationManager.ConnectionStrings["defautconnection"].ProviderName
Upvotes: -1
Reputation: 3677
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;
Upvotes: 18