开发者

ReSharper show warning with SqlDataReader

开发者 https://www.devze.com 2023-01-03 03:57 出处:网络
When I write something like this: using (var connection = new SqlConnection(\"ConnectionString\")) { using(var cmd= new SqlCommand(\"Command\"))

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 开发者_StackOverflow中文版null.

But in what cases can it be null? As I know if command returns nothing reader is not null, it only have nothing.


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 file System.Data\System.Data.Nullness.xml which contains this annotation:

<member name="M:System.Data.SqlClient.SqlCommand.ExecuteReader">
  <attribute ctor="M:JetBrains.Annotations.CanBeNullAttribute.#ctor" />
</member>

If by your own inspection you are satisfied that this is wrong, and that SqlCommand.ExecuteReader never returns null, you should change this to

<member name="M:System.Data.SqlClient.SqlCommand.ExecuteReader">
  <attribute ctor="M:JetBrains.Annotations.NotNullAttribute.#ctor" />
</member>


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消