Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 265
AndrewPolland Avatar asked Apr 02 '14 08:04

AndrewPolland


Video Answer


1 Answers

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;
like image 112
st mnmn Avatar answered Sep 19 '22 14:09

st mnmn