Visual Studio 连接字符串存放在哪里

Where Does Visual Studio Store the Connection String

问题是 table 适配器一直在引用我尚未为其设置的连接字符串。当我转到 DataSet Designer 中的每个数据 table 时,连接显示 "MyConnectionString(settings)"。当我搜索错误的连接字符串时,VS 找不到它。

在多个解决方案中重复使用的项目。我有三种配置:Debug、Staging 和 Release。每个配置都有自己的连接字符串。我的 app.config 看起来像这样:

 <?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>

每个配置文件看起来像这样:

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

在我的数据集中,我有这个 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>

在我的 settings.designer.cs 中,我有这个:

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

这个流氓连接字符串来自哪里?任何帮助、想法、建议和意见将不胜感激。

连接字符串存储在您的 app.config 文件中,有时存储在您的数据集中,有时存储在您的代码中。在我的例子中,我能够通过进入资源管理器并删除我不小心创建的所有文件(即 Form1)并通过搜索我的解决方案并确保没有引用不正确的连接字符串来解决这个问题。然后我删除了我在不同解决方案中包含该项目时使用的 .DLL 的所有实例,并重新引用并重建了所有项目。

还有machine.config,它是您系统上的主配置文件。这可能是您存储隐藏连接字符串的地方。

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.