我目前正在开发的一个程序收集数据并将其导出到 SQL 数据库。如果数据库出现故障,我已经进行了编码,以便将数据写入 CSV 文件。它将是一个始终使用配置文件运行的应用程序,用户可以在进程运行时更改信息(例如数据库连接)。我遇到的问题是,一旦进入写入 CSV 文件的循环,在重新启动应用程序之前,我无法恢复将数据放入数据库中。以下是相关代码:
class MethodClass
{
public static void Method()
{
while (true)
{
ConfigurationManager.RefreshSection("appSettings");
//Normally Running Code that Generates the Data
//Tries to write data to database 3 times
if (dbWrite != 2 && dbWrite != 3)
{
dbWrite = 0;
}
while (dbWrite < 2)
{
try
{
db.TblData.InsertOnSubmit(newrecord);
db.SubmitChanges();
break;
}
catch
{
dbWrite++;
}
}
if (dbWrite == 2)
{
try
{
db.TblData.InsertOnSubmit(newrecord);
db.SubmitChanges();
}
catch (Exception e)
{
dbWrite++;
log.Warn("Database Write Error", e);
BackupMethod();
}
}
if (dbWrite == 3)
{
try
{
db.TblData.InsertOnSubmit(newrecord);
db.SubmitChanges();
dbWrite = 0;
}
catch
{
BackupMethod();
log.Info("Still not writing to Database");
}
}
}
}
Class ConfigVariables
{
public static string SqlUser = ConfigurationManager.appSettings["SqlUser"];
//Other Config Variables
}
作为参考,我刚刚检查了一下,问题似乎是在程序运行时从我的配置文件中获取信息。如果我更改配置文件中的其他值,它们将在下次运行程序时才会生效。众所周知,我正在编辑 Debug/Release 文件夹中的 App.exe.config 文件。
更新:似乎如果我在 之后分配变量RefreshSection();
,它将起作用。但是,出于某种原因,我将变量放在单独的类中。有一个更好的方法吗?