简体   繁体   中英

Where Does Visual Studio Store the Connection String

The problem is that the table adapter keeps referencing a connection string that I have not set up for it. When I go to each data table in the DataSet Designer, the connect says "MyConnectionString(settings)". When I search for the incorrect connection string, VS can't find it.

The project that is reused over multiple solutions. I have three configurations: Debug, Staging and Release. Each configuration has it's own connection string. My app.config looks like this:

 <?xml version="1.0" encoding="utf-8" ?>
 <configuration>
   <connectionStrings configSource="connect.config"/>
   <startup>
     <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
   </startup>
   <startup useLegacyV2RuntimeActivationPolicy="true">
     <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" />
   </startup>
 </configuration>

Each configuration file looks something like this:

<connectionStrings>
   <clear/>
   <add name="Properties.Settings.MyConnectionString" connectionString="Data Source=CorrectDataSourceforthisConfig\SQL;Initial Catalog=MyDB;Trusted_Connection=True"
  providerName="System.Data.SqlClient" />
</connectionStrings>

In my dataset, I have this XML:

 <Connections>
      <Connection AppSettingsObjectName="Settings" AppSettingsPropertyName="MyConnectionString" ConnectionStringObject="" IsAppSettingsProperty="true" Modifier="Assembly" Name="MyConnectionString (Settings)" ParameterPrefix="@" PropertyReference="ApplicationSettings.MyMenu.Properties.Settings.GlobalReference.Default.MyConnectionString" Provider="System.Data.SqlClient" />
    </Connections>

In my settings.designer.cs, I have this:

    [global::System.Configuration.DefaultSettingValueAttribute("Data Source=CorrectDataSourceForDebug\SQL;Initial Catalog=MyDB;Integrated Security=True")]
    public string RMSConnectionString {
        get {
            return ((string)(this["MyConnectionString"]));
        }
    }

Where is this rogue connection string coming from? Any help, ideas, advice and opinions would be greatly appreciated.

The connection string is stored in your app.config file as well as in your sometimes in your dataset and sometimes in your code. In my case, I was able to fix this problem by going into Explorer and deleting all the files that I had accidentally created (ie Form1) and by searching my solution and making sure tha there were no reference to the incorrect connection string. Then I deleted all instances of the .DLL that I had used when I included this project in different solutions and re-referenced and rebuilt all the projects.

There is also machine.config which is the master configuration file on your system. This may be where your hidden connection string is stored.

https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/connection-strings-and-configuration-files

The machine.config file also contains a connectionStrings section, which contains connection strings used by Visual Studio. When retrieving connection strings by provider name from the app.config file in a Windows application, the connection strings in machine.config get loaded first, and then the entries from app.config. Adding clear immediately after the connectionStrings element removes all inherited references from the data structure in memory, so that only the connection strings defined in the local app.config file are considered.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM