.NET 2.0 App.Config connection strings includes unwanted SQLExpress default

馋奶兔 提交于 2019-12-19 09:27:04

问题


I'm using a connectionStrings section within an app.config file in a .NET 2.0 project. The config section contains two connection strings I have defined.

When I retrieve the ConnectionStringSettingsCollection it has a count of 3. The 0th entry is a connection to SQLExpress

Name: LocalSqlServer,
ConnectionString: data source=.\\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true

Why is this connection being included even though it's not in my app.config, and how can I get rid of it? This code will be running on desktop machines that I have no access to - so how can I prevent any more local connections from randomly showing up at runtime? I can't see any properties on the connection that indicate it's any different from the two I defined.


回答1:


It's defined in machine.config. To get rid of it, use:

<connectionStrings>
    <clear/>
    <add ... >
</connectionStrings>

As a general rule, sections that have <add> and <remove> elements (e.g. appSettings, connectionStrings and the provider configuration sections) also have a <clear> element, which you can use if you don't want to inherit elements from a higher level web.config or machine.config file.



来源:https://stackoverflow.com/questions/7573453/net-2-0-app-config-connection-strings-includes-unwanted-sqlexpress-default

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!