当将SqlCommand.ExecuteReader()返回null?

编程入门 行业动态 更新时间:2024-10-09 22:22:24
本文介绍了当将SqlCommand.ExecuteReader()返回null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在使用调用 SqlCommand.ExecuteReader()方法,ReSharper的告诉我,我有可能NullReference例外,当我使用SqlDataReader对象之后。

When using calling the SqlCommand.ExecuteReader() method, ReSharper tells me I have a possible NullReference exception when I use the SqlDataReader object afterwards.

因此​​,与下面的code:

So with the following code:

using (SqlConnection connection = GetConnection()) { using (SqlCommand cmd = connection.CreateCommand()) { cmd.CommandText = ; //snip using (SqlDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { //snip } } } }

在而(reader.Read())行带有下划线。

我的问题是,当将读者对象永远是空?我从来没有遇到过的文档中并没有提到它可以。我应该检查它是否为空或者是安全的忽略?

My question is when would the reader object ever be null? I've never come across it and the documentation doesn't mention that it could be. Should I be checking if it's null or is it safe to ignore?

和为什么ReSharper的认为,这可能是为空,例如当它让我使用SqlCommand不推荐它来检查空?我想有上ExecuteReader方法的属性。

And why would ReSharper think that it could be null, when for example it lets me use the SqlCommand without recommending it be checked for null? I'm guess there's an attribute on the ExecuteReader method.

推荐答案

这是一个误报。

反思SqlDataReader.ExecuteReader,我可以看到读者返回空值的唯一方法是,如果内部RunExecuteReader方法传递假的returnStream,它不是。

Reflecting on SqlDataReader.ExecuteReader, I can see that the only way the reader is returned as null is if the internal RunExecuteReader method is passed 'false' for returnStream, which it isn't.

在SqlDataReader的深处, A 阅读器的构造函数总是被调用在某些时候,所以我pretty的肯定,这是不是身体可能的ExecuteReader返回空值。

In the depths of SqlDataReader, a reader constructor is always called at some point, so I'm pretty sure this is not physically possible for ExecuteReader to return null.

更多推荐

当将SqlCommand.ExecuteReader()返回null?

本文发布于:2023-11-09 11:48:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1572297.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:SqlCommand   ExecuteReader   null

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!