使用 SQLCommand.EndExecuteNonQuery 时出现 SQL 语法错误
我正在尝试在后台工作程序中异步运行两个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,在两个 SQL 命令中添加 WAITFOR DELAY 似乎已经解决了这个问题。
有谁知道为什么会发生这种情况?
Ok, adding WAITFOR DELAY's to both SQL commands seems to have resolved the issue.
Anyone know why this happens?