ReSharper 使用 SqlDataReader 显示警告
当我写这样的内容时:
using (var connection = new SqlConnection("ConnectionString"))
{
using(var cmd= new SqlCommand("Command"))
{
using (var reader = cmd.ExecuteReader())
{
while (reader.Read())
{
}
}
}
}
ReSharper 在 reader.Read()
上显示警告,并告诉 reader 可以为 null
。
但什么情况下可以为空呢?据我所知,如果命令返回任何内容 reader 不为空,则它什么也没有。
When I write something like this:
using (var connection = new SqlConnection("ConnectionString"))
{
using(var cmd= new SqlCommand("Command"))
{
using (var reader = cmd.ExecuteReader())
{
while (reader.Read())
{
}
}
}
}
ReSharper shows warning on reader.Read()
, and tells that reader can be null
.
But in what cases can it be null? As I know if command returns nothing reader is not null, it only have nothing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
鉴于此在 YouTrack 上被报告为错误(两次 - 此处 和 此处),看起来 R# 附带的无效注释在这方面存在缺陷。
查看您的
ReSharper_installation_dir\Bin\ExternalAnnotations
文件夹 - 这是注释文件的安装位置。对于我来说,在 v5 中,这里有一个文件System.Data\System.Data.Nullness.xml
,其中包含以下注释:如果通过您自己的检查,您认为这是错误的,并且
SqlCommand.ExecuteReader
永远不会返回 null,您应该将其更改为Given that this is reported on YouTrack as a bug (twice - here and here), it looks like the nullity annotations that are shipped with R# are deficient in this respect.
Have a look in your
ReSharper_installation_dir\Bin\ExternalAnnotations
folder - this is where the annotations files get installed. For me, with v5, there is in here a fileSystem.Data\System.Data.Nullness.xml
which contains this annotation:If by your own inspection you are satisfied that this is wrong, and that
SqlCommand.ExecuteReader
never returns null, you should change this to我认为它不能为 null - 但 resharper 不知道
SqlCommand.ExeuteReader()
永远不会返回 null。据我所知,他们使用一些 xml 来告诉女巫函数可能会或可能不会返回 null,并且可以在那里设置它。
但我不知道 xml 的名称和位置。
I think it cannot be null - but resharper does not know that
SqlCommand.ExeuteReader()
will never return null.As far as I know there is some xml they use to tell witch functions may or may not return null and it can be set there.
I do not know the name and location of the xml though.