【发布时间】:2014-10-16 23:33:30
【问题描述】:
使用下面的代码,实际上只有第一组数据库命令被执行(一个表被删除,其他表删除了一条记录)如果在 DropTablesAndDeleteFromTables() 开头调用 MessageBox.Show()被注释掉了。
如果我取消注释,这样用户必须在每组数据库操作后关闭(显然不是我希望客户使用的版本),一切都很好 - 所有表都被删除,并且引用他们根据需要删除。为什么以这种方式中断进程(computus interruptus?)会影响成功与失败,我怎样才能吃到我的馅饼(让所有数据库命令成功而不用 N MessageBox.Show 打扰用户) () 要关闭的对话框?
private void DropTablesAndDeleteFromTables(string recordType, string fileName)
{
MessageBox.Show(String.Format("In DropTablesAndDeleteFromTables(), recordType is {0}, fileName is {1}", recordType, fileName)); //TODO: Remove
try
{
WorkFiles wrkFile = new WorkFiles();
int tableOK = 0;
DataSet workfiles;
tableOK = wrkFile.isValidWorkTable();
if (tableOK > 0) //Table has at least one record
{
workfiles = wrkFile.getAllRecords();
//Go thru dataset and find filename to clean up after
foreach (DataRow row in workfiles.Tables[0].Rows)
{
string tmpType = row["fileType"].ToString();
if (tmpType.EndsWith("0") || tmpType.EndsWith("1"))
{
tmpType = tmpType.Substring(0, 3);
}
string tmpStr = row["Name"].ToString();
int intSite = (int) row["siteNo"];
string tmpName = tmpType + "_" + intSite.ToString() + "_" + tmpStr;
if (tmpType != recordType) continue;
if (tmpName != fileName) continue;
//Drop workTables table from site-specific DB [ such as from HHSDB003.SDF ]
String dropTable = "DROP TABLE " + tmpType + tmpStr;
String delWorkTableSimple = string.Format("DELETE FROM workTables WHERE filetype = '{0}' and Name = '{1}'", tmpType, tmpStr);
String delWorkTable0 = "DELETE FROM workTables WHERE filetype = '" + tmpType + "0' and Name = '" + tmpStr + "'";
String delWorkTable1 = "DELETE FROM workTables WHERE filetype = '" + tmpType + "1' and Name = '" + tmpStr + "'";
// Do site-specific database first
// 0) Drop the table whose contents have been sent
SendCommandToDB(dropTable, intSite, true);
PauseThatRefreshes();
// 1) Delete record from site-specific [ HHSDB[siteNum].SDF workTables, such as HHSDB003.SDF ]
SendCommandToDB(delWorkTableSimple, intSite, true);
PauseThatRefreshes();
// Bypassing the "0" and "1" tables did nothing - still only drops one table and deletes
// 2) Same as 1, but for table named [DSD,INV}0_Bla
SendCommandToDB(delWorkTable0, intSite, true);
PauseThatRefreshes();
// 3) Same as 2, but for table named [DSD,INV}1_Bla instead of [DSD,INV}0_Bla
SendCommandToDB(delWorkTable1, intSite, true);
PauseThatRefreshes();
// Four calls to site-specific above; Three-four calls to NON-site-specific below
// 4) Delete record from NON-site-specific [ HHSDB[siteNum].SDF workTables, such as HHSDB003.SDF ]
SendCommandToDB(delWorkTableSimple, intSite, false);
PauseThatRefreshes();
// 5) Same as 1, but for table named [DSD,INV}0_Bla
SendCommandToDB(delWorkTable0, intSite, false);
PauseThatRefreshes();
// 6) Same as 2, but for table named [DSD,INV}1_Bla instead of [DSD,INV}0_Bla
SendCommandToDB(delWorkTable1, intSite, false);
PauseThatRefreshes();
// 7) Conditionally delete a record (if a DSD record, from DSDHeader, which is in the base (NON-site-specific) database
if (tmpType == "DSD")
{
String dml = string.Format("DELETE FROM {0}Header WHERE Name = '{1}'", tmpType, tmpStr);
SendCommandToDB(dml, intSite, false);
}
populateTransactionListBoxWithWorkTables();
return;
} // foreach (DataRow row in workfiles.Tables[0].Rows)
} // if ( tableOK > 0) //Table exist
//} // lock TFS#4054
} // try
catch (Exception ex)
{
SSCS.ExceptionHandler(ex, "frmCentral.DropTablesAndDeleteFromTables");
}
} // DropTablesAndDeleteFromTables
private void PauseThatRefreshes()
{
int j = 0;
while (j < 100000)
{
j++;
}
}
private void SendCommandToDB(String sql, int siteNum, bool SiteSpecificDB)
{
try
{
if (SiteSpecificDB)
{
if (dbconn.InBaseDatabase())
{
dbconn = DBConnection.GetInstance(siteNum.ToString());
}
}
else
{
if (!(dbconn.InBaseDatabase()))
{
dbconn = DBConnection.GetInstance();
}
}
dbconn.DBCommand(sql, true);
}
catch (SqlCeException ee)
{
. . .
}
}
有什么解决方法可以在不强制用户在游戏中扮演角色的情况下让流程浮出水面?
更新
这似乎是每组数据库操作之间经过多少时间的问题。当我改变这个时:
while (i < 100000)
...在 PauseThatRefreshes() 中对此:
while (i < 10000000)
(将 MessageBox.Show() 注释掉)它起作用了!但这仍然让我感到紧张。有没有更“科学”(优雅?)的方式来实现这一点?
【问题讨论】:
标签: c# sql-server-ce compact-framework windows-ce messagebox