代码之家  ›  专栏  ›  技术社区  ›  user113476

管理调试和释放连接字符串

  •  11
  • user113476  · 技术社区  · 16 年前

    在.NET/SQLServer应用程序中管理调试和释放连接字符串的好方法是什么?

    我有两个SQL服务器,一个是生产服务器,一个是构建/调试服务器,在部署ASP.NET应用程序时,我需要一种在这两个服务器之间切换的方法。

    8 回复  |  直到 12 年前
        1
  •  15
  •   AxelEckenberger    16 年前

    创建Web.config文件的调试和发布版本,例如Web.Debug.config和Web.Release.config。然后添加一个预编译条件,根据当前目标将相关版本复制到web.config中。

    要添加预编译条件,请右键单击项目并选择“属性”,然后转到“构建事件”选项卡并将下面的代码添加到预编译条件。显然,您必须根据自己的需要修改代码,请参见下图。

    @echo off
    
    echo Configuring web.config pre-build event ...
    
    if exist "$(ProjectDir)web.config" del /F / Q "$(ProjectDir)web.config"
    
    if "$(ConfigurationName)" == "Debug Test" goto test
    if "$(ConfigurationName)" == "Debug M" goto M
    if "$(ConfigurationName)" == "Debug BA" goto BA
    if "$(ConfigurationName)" == "Release Test" goto test
    if "$(ConfigurationName)" == "Release M" goto M
    if "$(ConfigurationName)" == "Release BA" goto BA
    
    echo No web.config found for configuration $(ConfigurationName). Abort batch.
    exit -1
    goto :end
    
    :test
    copy /Y "$(ProjectDir)web.config.test" "$(ProjectDir)web.config"
    GOTO end
    
    :BA
    copy /Y "$(ProjectDir)web.config.BA" "$(ProjectDir)web.config"
    GOTO end
    
    :M
    copy /Y "$(ProjectDir)web.config.M" "$(ProjectDir)web.config"
    GOTO end
    
    :end
    echo Pre-build event finished
    

    Project Properties http://img442.imageshack.us/img442/1843/propsa.jpg

        2
  •  8
  •   Henk Holterman    16 年前

    a provision 为此,可以为每个配置分别配置(web.Release.config、web.Debug.config)。

    坏消息是。。。你可能还没用。

        3
  •  4
  •   Sameh Deabes    16 年前

    在visualstudio中,您会注意到,语句只根据项目配置(debug或release)而暗显。

    只需在代码中添加以下内容:

    string myConnectionString;
    #if DEBUG
         myConnectionString = "your debug connection string";//may be read from your debug connection string from the config file
    #else
         myConnectionString = "your release connection string"; //may be read from your relase connection string from the config file
    #endif
    

    this .

        4
  •  2
  •   jjxtra    16 年前

    我通常在生产服务器上设置一个环境变量,表示该服务器是生产服务器。然后,根据此环境变量是否存在并设置为生产值,从web.config读取正确的连接字符串。

        5
  •  1
  •   chris.w.mclean    16 年前

    我在.NET3.5中结合使用了Sameh和Obalix的方法。

        public static class DataConnection
    {
    #if LOCALDEV
        public const string Env = "Debug";
    #endif
    #if STAGING
        public const string Env="Staging";
    #endif
    #if RELEASE
        public const string Env="Release";
    #endif
        private static ConnectionStringSettingsCollection _connections;
        static DataConnection()
        {
            _connections = ConfigurationManager.ConnectionStrings;
    
        }
    
        public static string BoloConnectionString
        {
            get
            {
                return _connections["DB1."+Env].ConnectionString;
            }
        }
        public static string AOAConnectionString
        {
            get
            {
                return _connections["DB2."+Env].ConnectionString;
            }
        }
        public static string DocVueConnectionString
        {
            get
            {
                return _connections["DB3."+Env].ConnectionString;
            }
        }
    }
    

        6
  •  0
  •   Hassan Syed    16 年前

    也许这有点过时,但是 ODBC DSN 很好地解决了这个问题--我仍然严格地使用--DNS设置来区分生产环境和调试环境。

    p、 在美国,我期待着大量的反对票,也许这将是一个衡量人们对数据库标识符间接性水平的标准。

        7
  •  0
  •   Gurgen Hovsepyan    14 年前

    <Content Include="DB.config">
      <SubType>Designer</SubType>
    </Content>
        <Content Include="DB.Debug.config">
      <DependentUpon>DB.config</DependentUpon>
      <SubType>Designer</SubType>
    </Content>
        <Content Include="DB.Release.config">
      <DependentUpon>DB.config</DependentUpon>
      <SubType>Designer</SubType>
    </Content>
    

    在编写的xml中,将两个版本设置为release和debug。

        8
  •  0
  •   Jordan Ryder    8 年前

    截至2018年,对于较新版本的visualstudio,微软已经接管了SlowCheetah扩展。安装此选项后,您可以选择将app.config文件拆分为三个单独的文件。一个是一个基本文件,其中包含始终应用的代码,然后您将得到一个app.debug.config文件和一个app.release.config文件。

    请注意,在项目中将其作为NuGet包拉入是不够的。如果您想要visualstudioui菜单选项,您需要从下面的站点下载安装程序并运行它。然后,在任何项目上安装SlowCheetah,您可以在使用NuGet时使用它。

    另外请注意,原始开发人员的原始SlowCheetah程序仍然存在,但请使用Microsoft发布的用于更新版本的VisualStudio。

    https://marketplace.visualstudio.com/items?itemName=VisualStudioProductTeam.SlowCheetah-XMLTransforms

    Add Transform

    Split Config

        9
  •  0
  •   BornToCode    7 年前

    <connectionStrings>
      <add name="myConnectionString" connectionString="myConnectionString" xdt:Transform="SetAttributes" xdt:Locator="Match(name)" />
    </connectionStrings>"
    

    2.编辑csproj并添加TransformXml目标:

    <Target Name="TransformActiveConfiguration" Condition="Exists('$(ProjectDir)/Web.$(Configuration).config')" BeforeTargets="Compile" >
        <TransformXml Source="$(ProjectDir)/Web.Config" Transform="$(ProjectDir)/Web.$(Configuration).config" Destination="$(TargetDir)/Web.config" />
    </Target>
    

    第二步将在每个构建(根据您的活动配置)上进行转换,而不仅仅是在发布上,从而为您提供更好的调试体验。我是从学校学的 this post .