FromAsyncPattern和SQLDataReader [英] FromAsyncPattern and SQLDataReader

查看:158
本文介绍了FromAsyncPattern和SQLDataReader的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好


我正在尝试使用上面的模式来包装sqldatareader。下面的最后一行导致问题

使用cmd = connection.CreateCommand 
cmd.CommandType = CommandType.Text
cmd.CommandText = script.ToSql(New SqlServerWriter)
connection.Open()
Dim asyncReader = Observable.FromAsyncPattern(Of SqlDataReader)(cmd.BeginExecuteReader,cmd.EndExecuteReader)

因为VB编译器抱怨EndExecuteReader需要参数asyncResult的参数。我已经检查了几个C#示例,这些示例似乎没有这样的参数。 


一定是显而易见的我缺少的东西。如果有人可以在这里指出我正确的VB实现,我将不胜感激。


很多thx


S



解决方案

< blockquote>

 


> VB编译器抱怨EndExecuteReader需要参数asyncResult的参数

b

根据文档,这里是 EndExecuteReader 的签名:


http:// msdn。 microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.endexecutereader.aspx

 public SqlDataReader EndExecuteReader(IAsyncResult asyncResult)

显然,有一个 IAsyncResult 参数。  (我检查了.NET Framework的所有版本以确保,因为您没有指定版本。)


所以问题必须是 FromAsyncPattern重载决议


唯一的另一个参数是 BeginExecuteReader ,所以我们来看看。  每个版本的.NET  Framework都有4个。重载&NBSP;以下是根据文档签名:


http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.beginexecutereader.aspx

 IAsyncResult BeginExecuteReader()
IAsyncResult BeginExecuteReader(CommandBehavior)
IAsyncResult BeginExecuteReader(AsyncCallback,Object)
IAsyncResult BeginExecuteReader(AsyncCallback,Object,CommandBehavior)

后两者符合APM的要求。  或许他们对
FromAsyncPattern 的重载解析不明确,或者编译器只是选择错了。 那么让我们看看
FromAsyncPattern 的重载是候选者......


你指定了一个泛型类型参数,所以这里是签名(根据文档,


http://msdn.microsoft.com/en-us/library/hh229919(v=vs。 103).aspx

 FromAsyncPattern< TResult>(Func< AsyncCallback,Object,IAsyncResult>,Func< IAsyncResult,TResult>)
FromAsyncPattern< ; T1>(Func< T1,AsyncCallback,Object,IAsyncResult>,Action< IAsyncResult>)

查看参数的数量和顺序,似乎只有第一个签名是有效的匹配,它只能对应
的第三个签名 BeginExecuteReader

 IAsyncResult BeginExecuteReader(AsyncCallback,Object)

请注意,第二个签名与
的第四个签名 BeginExecuteReader 因为 CommandBehavior 位于错误的位置,这会将
对象放在需要 AsyncCallback 的位置,尽管它具有正确的数量泛型类型参数。


所以看起来我们已经获胜了。 但令我惊讶的是,它还匹配
EndExecuteReader 的签名。

 IAsyncResult BeginExecuteReader(AsyncCallback,Object)
SqlDataReader EndExecuteReader(IAsyncResult asyncResult)
FromAsyncPattern< TResult>(Func< AsyncCallback,Object,IAsyncResult>,Func< IAsyncResult,TResult>)

显然,我没有不要在Visual Studio中尝试这个。 如果将鼠标光标放在
FromAsyncPattern 上,那么IntelliSense显示编译器正在选择哪个重载?


- Dave


Hi

I'm trying to use the above pattern to wrap the sqldatareader. The last line below is causing a problem

Using cmd = connection.CreateCommand
      cmd.CommandType = CommandType.Text
      cmd.CommandText = script.ToSql(New SqlServerWriter)
      connection.Open()
      Dim asyncReader = Observable.FromAsyncPattern(Of SqlDataReader)(cmd.BeginExecuteReader, cmd.EndExecuteReader)

since the VB compiler is complaining that EndExecuteReader needs an argument for parameter asyncResult. I have checked several C# examples which don't seem to have such a parameter. 

Must be something obviously I'm missing. If someone could point me to a correct VB implementation here, I'd be grateful.

Many thx

S

解决方案

Hi, 

> the VB compiler is complaining that EndExecuteReader needs an argument for parameter asyncResult

Here's the signature of EndExecuteReader according to the documentation:

http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.endexecutereader.aspx

public SqlDataReader EndExecuteReader(IAsyncResult asyncResult)

Clearly there's an IAsyncResult parameter.  (I checked all versions of the .NET Framework to be sure, since you didn't specify a version.)

So the problem must be with overload resolution of FromAsyncPattern.

The only other argument is BeginExecuteReader, so let's have a look at that.  Each version of the .NET Framework has 4 overloads.  Here are the signatures according to the documentation:

http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.beginexecutereader.aspx

IAsyncResult BeginExecuteReader()
IAsyncResult BeginExecuteReader(CommandBehavior)
IAsyncResult BeginExecuteReader(AsyncCallback, Object)
IAsyncResult BeginExecuteReader(AsyncCallback, Object, CommandBehavior)

The latter two meet the requirements for the APM.  Perhaps they're ambiguous for overload resolution of FromAsyncPattern, or the compiler is simply choosing the wrong one.  So let's see what overloads of FromAsyncPattern are candidates...

You're specifying a single generic type argument, so here are the signatures (excluding return types) of FromAsyncPattern that have a single generic type argument according to the documentation:

http://msdn.microsoft.com/en-us/library/hh229919(v=vs.103).aspx

FromAsyncPattern<TResult>(Func<AsyncCallback, Object, IAsyncResult>, Func<IAsyncResult, TResult>)
FromAsyncPattern<T1>(Func<T1, AsyncCallback, Object, IAsyncResult>, Action<IAsyncResult>)

Looking at the number and order of the parameters, it seems that only the first signature is a valid match and it can only correspond to the third signature of BeginExecuteReader:

IAsyncResult BeginExecuteReader(AsyncCallback, Object)

Note that the second signature doesn't match the fourth signature of BeginExecuteReader because CommandBehavior is in the wrong place, which would put Object where AsyncCallback is expected, though it has the correct number of generic type arguments.

So it looks like we've got our winner.  But to my surprise, it also matches the signature of EndExecuteReader.

IAsyncResult BeginExecuteReader(AsyncCallback, Object)
SqlDataReader EndExecuteReader(IAsyncResult asyncResult)
FromAsyncPattern<TResult>(Func<AsyncCallback, Object, IAsyncResult>, Func<IAsyncResult, TResult>)

Obviously, I didn't try this in Visual Studio.  If you place the mouse cursor over FromAsyncPattern, which overload does IntelliSense show that the compiler is choosing?

- Dave


这篇关于FromAsyncPattern和SQLDataReader的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆