首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >异步SQLCommand和CCR

异步SQLCommand和CCR
EN

Stack Overflow用户
提问于 2009-02-03 03:59:53
回答 1查看 721关注 0票数 0

我一直在使用Jeffrey Richter的这篇msdn文章中的演示代码。

我已经向他的ApmToCcrAdapters添加了一个新函数来处理SqlCommand.BeginExecuteReader。只有在我可以阅读之前,它才会关闭阅读器。

以下代码用于提供FromIteratorHandler:

代码语言:javascript
复制
    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"]);
       }
   }

它依次调用以下代码:

代码语言:javascript
复制
   /// <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);
   }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2009-02-03 04:22:21

连接必须保持打开状态,读卡器才能正常工作。我相信关闭连接是你的问题。让连接保持打开状态,并在完成后调用读取器上的dispose,我认为这应该会清理连接。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/505970

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档