Firebird ConnectionString 在运行时更改
Firebird ConnectionString Change during runtime
我正在开发一个应用程序,它有一个实时网络数据库和一个应急本地数据库,它检测实时网络数据库是否可以访问,如果不能访问,它会在三秒后超时,更改连接字符串到本地应急数据库。
根据此处的提示,我在 运行 时间 和 重新加载设置期间成功地更改了 app.config 上的连接字符串。
这是应用程序在需要更改连接字符串时调用的方法:
public static void ChangeConnectionString(string connectionstring)
{
var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var connectionStringsSection = (ConnectionStringsSection)config.GetSection("connectionStrings");
connectionStringsSection.ConnectionStrings[0].ConnectionString = connectionstring;
var connectionStrings = config.ConnectionStrings;
foreach (ConnectionStringSettings connectionString in connectionStrings.ConnectionStrings)
{
connectionString.ConnectionString = connectionstring;
}
config.Save();
ConfigurationManager.RefreshSection("connectionStrings");
PDV_WPF.Properties.Settings.Default.Save();
PDV_WPF.Properties.Settings.Default.Reload();
//Ensures the configuration is saved and reloaded.
FbConnection.ClearAllPools();
//Closes all currently open connections which might be using the old connection string.
Debug.WriteLine("==========Ran ChangeConnectionString");
Debug.WriteLine("==========FDBConnString is:");
Debug.WriteLine("==========" + PDV_WPF.Properties.Settings.Default.FDBConnString);
在我断开网络连接后,每当我检查当前的FDBConnString 时,它都正确地指向本地的应急数据库。但是,在下一行,当它尝试 运行 查询时,出现以下异常:
Inner Exception 1:
IscException: Unable to complete network request to host "dbserver".
Inner Exception 2:
SocketException: Este host não é conhecido //(This host is unknown)
完整异常详细信息:https://pastebin.com/3syLvsQf
看来,即使在我成功更改连接字符串并成功重新加载应用程序配置文件后,它仍然会尝试使用旧连接字符串打开连接。即使我调用调试在 FbConnection.Open() 调用上方的行上打印当前 Properties.Settings.Default.FDBConnString,它也会显示 新字符串 而不是不正确的,旧的。
对可能发生的事情有任何见解吗?
我发现了问题所在。
我正在从生成的 xsd 文件实例化一个继承的 table 适配器。当我在 class 上声明一个 table 适配器时,它还继承了声明时存储在 app.config 上的连接字符串。因此,如果我更改 app.config 并不重要,因为声明的 table 适配器已经卡在以前的连接字符串中。
所以,解决方案是,而不是更改存储在 app.config 上的连接字符串,我只需要更改声明的 table 适配器上的连接字符串:
tB_STOCKTableAdapter1.Connection.ConnectionString = Properties.Settings.Default.ContingencyDB;
tB_STK_PRODUCTTableAdapter1.Connection.ConnectionString = Properties.Settings.Default.ContingencyDB;
或
tB_STOCKTableAdapter1.Connection.ConnectionString = Properties.Settings.Default.NetworkDB;
tB_STK_PRODUCTTableAdapter1.Connection.ConnectionString = Properties.Settings.Default.NetworkDB;
ContingencyDB 和 NetworkDB 都是作为用户范围字符串存储在 app.config 上的字符串,可以通过给定的 "Settings" window 呈现给用户进行更改。
我正在开发一个应用程序,它有一个实时网络数据库和一个应急本地数据库,它检测实时网络数据库是否可以访问,如果不能访问,它会在三秒后超时,更改连接字符串到本地应急数据库。
根据此处的提示,我在 运行 时间 和 重新加载设置期间成功地更改了 app.config 上的连接字符串。
这是应用程序在需要更改连接字符串时调用的方法:
public static void ChangeConnectionString(string connectionstring)
{
var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var connectionStringsSection = (ConnectionStringsSection)config.GetSection("connectionStrings");
connectionStringsSection.ConnectionStrings[0].ConnectionString = connectionstring;
var connectionStrings = config.ConnectionStrings;
foreach (ConnectionStringSettings connectionString in connectionStrings.ConnectionStrings)
{
connectionString.ConnectionString = connectionstring;
}
config.Save();
ConfigurationManager.RefreshSection("connectionStrings");
PDV_WPF.Properties.Settings.Default.Save();
PDV_WPF.Properties.Settings.Default.Reload();
//Ensures the configuration is saved and reloaded.
FbConnection.ClearAllPools();
//Closes all currently open connections which might be using the old connection string.
Debug.WriteLine("==========Ran ChangeConnectionString");
Debug.WriteLine("==========FDBConnString is:");
Debug.WriteLine("==========" + PDV_WPF.Properties.Settings.Default.FDBConnString);
在我断开网络连接后,每当我检查当前的FDBConnString 时,它都正确地指向本地的应急数据库。但是,在下一行,当它尝试 运行 查询时,出现以下异常:
Inner Exception 1:
IscException: Unable to complete network request to host "dbserver".
Inner Exception 2:
SocketException: Este host não é conhecido //(This host is unknown)
完整异常详细信息:https://pastebin.com/3syLvsQf
看来,即使在我成功更改连接字符串并成功重新加载应用程序配置文件后,它仍然会尝试使用旧连接字符串打开连接。即使我调用调试在 FbConnection.Open() 调用上方的行上打印当前 Properties.Settings.Default.FDBConnString,它也会显示 新字符串 而不是不正确的,旧的。
对可能发生的事情有任何见解吗?
我发现了问题所在。 我正在从生成的 xsd 文件实例化一个继承的 table 适配器。当我在 class 上声明一个 table 适配器时,它还继承了声明时存储在 app.config 上的连接字符串。因此,如果我更改 app.config 并不重要,因为声明的 table 适配器已经卡在以前的连接字符串中。
所以,解决方案是,而不是更改存储在 app.config 上的连接字符串,我只需要更改声明的 table 适配器上的连接字符串:
tB_STOCKTableAdapter1.Connection.ConnectionString = Properties.Settings.Default.ContingencyDB;
tB_STK_PRODUCTTableAdapter1.Connection.ConnectionString = Properties.Settings.Default.ContingencyDB;
或
tB_STOCKTableAdapter1.Connection.ConnectionString = Properties.Settings.Default.NetworkDB;
tB_STK_PRODUCTTableAdapter1.Connection.ConnectionString = Properties.Settings.Default.NetworkDB;
ContingencyDB 和 NetworkDB 都是作为用户范围字符串存储在 app.config 上的字符串,可以通过给定的 "Settings" window 呈现给用户进行更改。