C# Download file from MSSql database column as stream

Date: 2019-12-04
public async Task<Stream> DownloadById(Guid workorderattachmentid)
{
	using (var connection = UnitOfWork.GetContext().Database.GetDbConnection()) {
		await connection.OpenAsync();
	
		var command = connection.CreateCommand();
		command.CommandText = "SELECT Data FROM WorkOrderAttachment WHERE Id = @id";
		var param = command.CreateParameter();
		param.ParameterName = "@id";
		param.Value = workorderattachmentid.ToString();
		command.Parameters.Add(param);

		var reader = await command.ExecuteReaderAsync(CommandBehavior.CloseConnection);
		await reader.ReadAsync(); 
		if (await reader.IsDBNullAsync(0))
				return null;
		var stream = reader.GetStream(0);
		return stream;
	}
}
30100cookie-checkC# Download file from MSSql database column as stream