Currently, my application has connection strings hard coded.
string connectionString2 = "Data Source=SWDB10DSQL;Initial Catalog=BillingUI;Integrated Security=True;MultipleActiveResultSets=True;Application Name=EntityFramework";
I'm trying to change it so I reference the connection string that is defined in the web.config file instead. I've tried a solution found on here below, however it's not working (data is not inserting into my table anymore). It compiles however.
ImportController.cs
string connectionString2 = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["BillingUIEntities"].ConnectionString;
using (SqlConnection _con2 = new SqlConnection(connectionString2))
{
_con2.Open();
Web.config
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
<add name="BillingUIEntities" connectionString="metadata=res://*/Models.Model2.csdl|res://*/Models.Model2.ssdl|res://*/Models.Model2.msl;provider=System.Data.SqlClient;provider connection string="data source=SWDB10DSQL;initial catalog=BillingUI;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" /></connectionStrings>
Update: I've determined that it is getting the connection string from Web.config, but it's including the meta data which linq/ my sql connection doesn't seem to want in the controller. I thought sub stringing could be a work around but I don't think that's a good way of doing it cause the meta data can change, resulting in a longer/shorter length string which would cause me to substring an invalid region of the connection string. Is there a better way to do this?
Use ConfigurationManager instead of WebConfgurationManager.
string connectionString2 = System.Configuration.ConfigurationManager.ConnectionStrings["BillingUIEntities"].ConnectionString;
:) David
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.