使用 SQLCommand.EndExecuteNonQuery 时出现 SQL 语法错误

发布于 2024-10-12 12:06:41 字数 1565 浏览 8 评论 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); 上失败

I'm trying to run two SQL statements (MSSQL 2005), asynchronously in a background worker. However, when I call the EndExecuteNonQuery method on the first SqlCommand I get a 'SQL syntax error near' error.

Here is my code:

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));
}

It fails on the first count = sqlCmd.EndExecuteNonQuery(result);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倒带 2024-10-19 12:06:41

好吧,在两个 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);

有谁知道为什么会发生这种情况?

Ok, adding WAITFOR DELAY's to both SQL commands seems to have resolved the issue.

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);

Anyone know why this happens?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文