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

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

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

目前我只是将它们存储在web.config中并对其中一个或另一个进行注释,但这在部署时容易出错。

创建Web.config文件的调试和发布版本,例如Web.debug.config和Web.release.config。 然后添加一个预编译条件,根据当前Target将相关版本复制到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 

项目属性http://img442.imageshack.us/img442/1843/propsa.jpg

好消息是.NET4有这样的规定 ,你可以为每个配置(web.Release.config,web.Debug.config)分别配置。

坏消息是……你可能还没有使用它。

使用预处理程序指令:当您的项目配置为在调试模式下运行时,将选择调试连接字符串,否则将自动选择发布连接字符串。

在Visual Studio中,您会注意到语句仅根据项目配置(调试或发布)进行调暗。

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

 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 

有关更多详细信息,请检查此

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

我在.net 3.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; } } } 

然后在我的项目属性中,我定义了正确的条件编译符号。 这样我就不必像Sameh那样对连接字符串进行硬编码,但代码只根据字符串的构建方式查找字符串。 这让我(如果我需要)一个配置文件用于所有构建,但实际上我没有在我的构建过程中部署配置文件。 虽然.net 4的条件app.Relase.config东西看起来是未来的正确方法。

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

ps,我期待大量的下选票,也许这将衡量人们对数据库标识符的间接级别的看法。

我可以说这个问题的其他解决方案。 在csproj文件文件中创建folow:

  Designer   DB.config Designer   DB.config Designer  

在xml中编写了两个版本用于发布和调试。

截至2018年,对于较新版本的Visual Studio,Microsoft已经接管了SlowCheetah扩展。 安装此选项将为您提供将app.config文件拆分为三个单独文件的选项。 一个是基本文件,其中包含始终适用的代码,然后您将获得app.debug.config文件和app.release.config文件。

请注意,在项目中将其作为NuGet包拉出是不够的。 如果您需要Visual Studio UI菜单选项,则需要从下面的站点实际下载安装程序并运行它。 然后,使用NuGet专门在要使用它的任何项目上安装SlowCheetah。

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

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

添加变换

拆分配置