비동기 SQLCOMMAND 및 CCR
-
21-08-2019 - |
문제
이 MSDN 기사의 데모 코드를 사용하여 제프리 리히터.
SQLCommand.beginexecutereader를 처리하기 위해 그의 APMTocCradapters에 새로운 기능을 추가했습니다. 읽기 전에 독자를 닫는다.
다음 코드는 FromiteratorHandler를 제공하는 데 사용됩니다.
private static IEnumerator<ITask> AsyncReaderDemoHandler()
{
SqlDataReader reader = null;
SqlConnection connection = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=BizData;Integrated Security=True;Async=True;");
string query = "SELECT * FROM Account;";
SqlCommand command = new SqlCommand(query,connection);
connection.Open();
yield return Arbiter.Choice(ApmToCcrAdapters.GetReader(command),
delegate(SqlDataReader r) { Msg("Got SQL data"); reader = r; },
delegate(Exception e) { Msg("Failed to get SQL data"); });
connection.Close();
if (reader == null) yield break;
//This is where the code fails: Reader is Closed!
while (reader.Read())
{
Console.WriteLine(reader["Account"]);
}
}
결과적으로 다음 코드를 호출합니다.
/// <summary>
/// Gets the Reader, requires connection to be managed
/// </summary>
public static PortSet<SqlDataReader, Exception> GetReader(SqlCommand sqlCommand)
{
Port<SqlDataReader> portResponse = null;
Port<Exception> portException = null;
GetReaderResponse(sqlCommand, ref portResponse, ref portException);
return new PortSet<SqlDataReader, Exception>(portResponse, portException);
}
// Wrapper for SqlCommand's GetResponse
public static void GetReaderResponse(SqlCommand sqlCom,
ref Port<SqlDataReader> portResponse, ref Port<Exception> portException)
{
EnsurePortsExist(ref portResponse, ref portException);
sqlCom.BeginExecuteReader(ApmResultToCcrResultFactory.Create(
portResponse, portException,
delegate(IAsyncResult ar) { return sqlCom.EndExecuteReader(ar); }), null);
}
해결책
독자가 작동하기 위해 연결을 열어야합니다. 연결을 닫는 것이 당신의 문제라고 생각합니다. 연결을 열어 놓고 완료되면 독자에게 전화를 걸면 연결을 정리해야한다고 생각합니다.
제휴하지 않습니다 StackOverflow