0

我正在尝试在后台工作人员中异步运行两个 SQL 语句(MSSQL 2005)。但是,当我在第一个 SqlCommand 上调用 EndExecuteNonQuery 方法时,我收到“SQL 语法错误附近”错误。

这是我的代码:

try
{
    SqlCommand sqlCmd = uow.DataLayer.CreateCommand() as SqlCommand;
    sqlCmd.CommandText = "DELETE FROM dbo.EligibilityRecordKeyValue WHERE EligibilityRecord IN " +
    "(SELECT EligibilityRecord FROM dbo.EligibilityRecord WHERE Organization = '" + map.Organization.Oid + "')";
    IAsyncResult result = sqlCmd.BeginExecuteNonQuery();
    while (!result.IsCompleted)
    {
        worker.ReportProgress(0, "Deleting existing record keys");
        System.Threading.Thread.Sleep(200);
    }
    count = sqlCmd.EndExecuteNonQuery(result);
}
catch (SqlException ex)
{
}
catch (InvalidOperationException ex)
{
}
finally
{
    worker.ReportProgress(2, String.Format("Existing {0} records keys deleted.", count));
}

try
{
    SqlCommand sqlCmd = uow.DataLayer.CreateCommand() as SqlCommand;
    sqlCmd.CommandText = "DELETE FROM dbo.EligibilityRecord WHERE Organization = '" +      map.Organization.Oid + "'";
    IAsyncResult result = sqlCmd.BeginExecuteNonQuery();
    while (!result.IsCompleted)
    {
        worker.ReportProgress(0, "Deleting existing records");
        System.Threading.Thread.Sleep(200);
    }
    count = sqlCmd.EndExecuteNonQuery(result);
}
catch (SqlException ex)
{
}
catch (InvalidOperationException ex)
{
}
finally
{
    worker.ReportProgress(5, String.Format("Existing {0} records deleted.", count));
}

第一次失败count = sqlCmd.EndExecuteNonQuery(result);

4

1 回答 1

0

好的,在两个 SQL 命令中添加 WAITFOR DELAY 似乎已经解决了这个问题。

sqlCmd.CommandText = String.Format("WAITFOR DELAY '00:00:05'; DELETE FROM dbo.EligibilityRecordKeyValue WHERE EligibilityRecord IN " + 
                    "(SELECT EligibilityRecord FROM dbo.EligibilityRecord WHERE Organization = '{0}')", map.Organization.Oid);

sqlCmd.CommandText = String.Format("WAITFOR DELAY '00:00:05'; DELETE FROM dbo.EligibilityRecord WHERE Organization = '{0}'", map.Organization.Oid);

有谁知道为什么会这样?

于 2011-01-13T21:16:09.970 回答