Skip to content

GetFieldValueAsync<Stream> after IsDBNullAsync returns an empty stream #3057

@bbowyersmyth

Description

@bbowyersmyth

Describe the bug

With SequentialAccess, calling GetFieldValueAsync<Stream> on its own returns a readable stream. If IsDBNullAsync is called first then the stream will have no data.

Using GetStream instead returns a readable stream in both cases.

To reproduce

Based on GetFieldValueAsync_OfStream unit test

using (SqlDataReader reader = await command.ExecuteReaderAsync(CommandBehavior.SequentialAccess))
{
    if (await reader.ReadAsync() && !await reader.IsDBNullAsync(1))
    {
        using var buffer = new MemoryStream(originalData.Length);

        // Doesn't have any bytes to read
        using (Stream stream = await reader.GetFieldValueAsync<Stream>(1))
        {
            await stream.CopyToAsync(buffer);
            outputData = buffer.ToArray();
        }

        // Works
        //using (Stream stream = reader.GetStream(1))
        //{
        //    await stream.CopyToAsync(buffer);
        //    outputData = buffer.ToArray();
        //}
    }
}

Debug.Assert(originalData.SequenceEqual(outputData));

Expected behavior

Expect GetFieldValueAsync<Stream> to work the same as GetStream in that IsDBNullAsync doesn't consume the column

Further technical details

Microsoft.Data.SqlClient version: 5.2.2
.NET target: .NET 8
SQL Server version: 2022
Operating system: Windows 11

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

Status

In review

Relationships

None yet

Development

No branches or pull requests

Issue actions