您可以在App.config
文件中包含連接字符串。在應用程序部署過程中,您可以通過查詢registery
來檢查用戶機器是否安裝了SQL Server。基於這個結果更新應用程序配置與數據庫特定的連接字符串。在app.config
App.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="DbConnectionString" value="Will be updated during deployment" />
</appSettings>
</configuration>
更新連接字符串:
Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
configuration.AppSettings.Settings["DbConnectionString"].Value = "DB Specific Connection string";
//Save only the modified section of the config
configuration.Save(ConfigurationSaveMode.Modified);
//Refresh the appSettings section to reflect updated configurations
ConfigurationManager.RefreshSection(Constants.AppSettingsNode);
把它放在'app.config'文件。 –
它是一個Windows服務? – VahidNaderi
把連接字符串放在'app.config'文件中 – paul