开发者

What happens if I invoke the `ExecuteReader` method on a `SqlCommand` referencing a stored procedure that returns no rowsets?

I have a SqlCommand which may return zero or more rowsets. What happens if, by chance, the SqlCommand would return exactly zero rowsets* and I invoke its ExecuteReader method? Do I get a SqlDataReader that cannot be read, or do I get an exception?

Just in case: Zero rowsets is not the same 开发者_运维问答thing as one rowset containing exactly zero rows.


Your reader.FieldCount will be zero for no rowset. If you use a dataset instead of a reader, you will get a null dataset for no rowset.


SqlDataReader.Read() as well as HasRows will return false:

var reader = command.ExecuteReader();
if (reader.HasRows) // false
{
    while (reader.Read()) // false
    {
        // will never reach
    }
}


You should still receive an SqlDataReader object. when you try:

using (SqlCommand command = new SqlCommand( /* params */))
using (SqlDataReader reader = command.ExecuteReader())
{
    if (reader.HasRows)    // check to see if we have any rows
    {
        while (reader.Read())
        {
            // process
        }
    }
}

... you will simply 'fall through' as Read() will return false.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜