0
我正在编写的代码将数据从一个表复制到另一个表。有可能查询可能会运行很长时间,所以我正在执行异步查询,并在等待时正在对目标表进行计数以提供状态更新。在等待异步操作时执行SQL查询会生成异常
,做的次数也越来越以下异常信息查询......
命令执行无法继续因未处理的异步操作已在进行中。
很清楚我在做什么是不允许的。那么异步SQL操作的规则是什么?是否有另一种方式来获取挂起操作的状态?
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(_connectionString);
builder.AsynchronousProcessing = true;
using(SqlConnection connection = new SqlConnection(builder.ConnectionString))
{
connection.Open();
SqlCommand command = new SqlCommand();
command.Connection = connection;
command.CommandType = System.Data.CommandType.Text;
command.CommandText = "INSERT INTO DestTable SELECT * FROM SourceTable";
IAsyncResult result = command.BeginExecuteNonQuery();
using(SqlConnection statusConnection = new SqlConnection(_connectionString))
{
while(!result.IsCompleted)
{
SqlCommand statusCommand = new SqlCommand();
statusCommand.Connection = statusConnection;
statusCommand.CommandType = System.Data.CommandType.Text;
statusCommand.CommandText = "SELECT COUNT(*) FROM DestTable";
int currentRowCount = (int)command.ExecuteScalar();
Thread.Sleep(500);
}
command.EndExecuteNonQuery(result);
}
}
请使用您正在使用的数据库产品(Oracle,SQL Server等)标记此问题。 – 2010-10-22 01:20:28