Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么我的数据库命令只有在我使用MessageBox.Show()调用中断它们时才起作用?_C#_Sql Server Ce_Compact Framework_Windows Ce_Messagebox - Fatal编程技术网

C# 为什么我的数据库命令只有在我使用MessageBox.Show()调用中断它们时才起作用?

C# 为什么我的数据库命令只有在我使用MessageBox.Show()调用中断它们时才起作用?,c#,sql-server-ce,compact-framework,windows-ce,messagebox,C#,Sql Server Ce,Compact Framework,Windows Ce,Messagebox,在下面的代码中,如果DropTablesAndDeleteFromTables()开头的MessageBox.Show()调用被注释掉,那么实际上只有第一组数据库命令被执行到结果(一个表被删除,其他表中有一条记录被删除) 如果我取消对它的注释,以便用户在每次数据库操作后都必须关闭它(显然不是我希望客户使用的版本),那么一切都很好——所有表都会被删除,对它们的引用也会根据需要被删除。为什么以这种方式中断进程(computus interruptus?)会区分成功与失败,我如何才能吃到馅饼(让所有数

在下面的代码中,如果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)
    {
        . . .
    } 
}
private void droptables和deletefromtables(字符串记录类型,字符串文件名)
{
MessageBox.Show(String.Format(“在DropTablesAndDeleteFromTables()中,记录类型为{0},文件名为{1}”,记录类型为,文件名));//TODO:删除
尝试
{
WorkFiles wrkFile=新工作文件();
int tableOK=0;
数据集工作文件;
tableOK=wrkFile.isValidWorkTable();
if(tableOK>0)//表至少有一条记录
{
workfiles=wrkFile.getAllRecords();
//遍历数据集并查找文件名,然后进行清理
foreach(工作文件中的DataRow行。表[0]。行)
{
字符串tmpType=row[“fileType”].ToString();
if(tmpType.EndsWith(“0”)| | tmpType.EndsWith(“1”))
{
tmpType=tmpType.Substring(0,3);
}
字符串tmpStr=行[“名称”]。ToString();
int intSite=(int)行[“siteNo”];
字符串tmpName=tmpType+“”+intSite.ToString()+“”+tmpStr;
如果(tmpType!=记录类型)继续;
如果(tmpName!=文件名)继续;
//从特定于站点的数据库中删除工作表[例如从HHSDB003.SDF中删除]
String dropTable=“DROP TABLE”+tmpType+tmpStr;
String delWorkTableSimple=String.Format(“从文件类型为“{0}”且名称为“{1}”的工作表中删除”,tmpType,tmpStr);
String delWorkTable0=“从工作表中删除,其中filetype='”+tmpType+“0”和Name='“+tmpStr+””;
String delWorkTable1=“从工作表中删除,其中filetype='”+tmpType+“1”和Name='“+tmpStr+””;
//首先执行站点特定的数据库
//0)删除已发送内容的表
SendCommandToDB(dropTable,intSite,true);
pausethatreshes();
//1)从特定于站点的[HHSDB[siteNum].SDF工作表中删除记录,例如HHSDB003.SDF]
SendCommandToDB(delWorkTableSimple、intSite、true);
pausethatreshes();
//绕过“0”和“1”表没有任何作用-仍然只删除一个表并删除
//2)与1相同,但用于名为[DSD,INV}0_Bla的表
SendCommandToDB(delWorkTable0,intSite,true);
pausethatreshes();
//3)与2相同,但用于名为[DSD,INV}1_Bla的表,而不是[DSD,INV}0_Bla
SendCommandToDB(delWorkTable1,intSite,true);
pausethatreshes();
//四次呼叫特定于上述站点;三次呼叫四次呼叫非特定于以下站点
//4)从非站点特定的[HHSDB[siteNum].SDF工作表中删除记录,如HHSDB003.SDF]
SendCommandToDB(delWorkTableSimple、intSite、false);
pausethatreshes();
//5)与1相同,但用于名为[DSD,INV}0_Bla的表
SendCommandToDB(delWorkTable0,intSite,false);
pausethatreshes();
//6)与2相同,但用于名为[DSD,INV}1_Bla的表,而不是[DSD,INV}0_Bla
SendCommandToDB(delWorkTable1,intSite,false);
pausethatreshes();
//7)有条件地从基本(非站点特定)数据库中的DSDHeader中删除记录(如果是DSD记录)
如果(tmpType==“DSD”)
{
String dml=String.Format(“从{0}头中删除,其中Name='{1}',tmpType,tmpStr);
SendCommandToDB(dml、intSite、false);
}
populateTransactionListBoxWithWorkTables();
返回;
}//foreach(工作文件中的DataRow行。表[0]。行)
}//如果(tableOK>0)//表存在
//}//锁定TFS#4054
}//试试看
捕获(例外情况除外)
{
SSC.ExceptionHandler(例如,“frmCentral.DropTables和DeleteFromTables”);
}
}//DropTables和DeleteFromTables
私有void PauseThatRefreshes()
{
int j=0;
而(j<100000)
{
j++;
}
}
私有void sendcommandtob(字符串sql、int siteNum、bool SiteSpecificDB)
{
尝试
{
如果(SiteSpecificDB)
{
if(dbconn.InBaseDatabase())
{
dbconn=DBConnection.GetInstance(siteNum.ToString());
}
}
其他的
{
if(!(dbconn.InBaseDatabase())
{
dbconn=DBConnection.GetInstance();
}
}
dbconn.DBCommand(sql,true);
}
捕获(SQLCEEE异常)
{
. . .
} 
}
什么是一个变通方法,让这个过程在不强迫用户扮演角色的情况下公之于众

更新 这似乎是一个关于每一组数据库操作之间经过多少时间的问题。当我改变这一点时:

while (i < 100000)
while(i<100000)
…在PauseThatRefreshes()中,将其更改为:

while(i<10000000)
(wi)
while (i < 10000000)