Microsoft Azure Stack is an extension of Azure—bringing the agility and innovation of cloud computing to your on-premises environment and enabling the only hybrid cloud that allows you to build and deploy hybrid applications anywhere. We bring together the best of the edge and cloud to deliver Azure services anywhere in your environment.

Streaming Blobs To and From SQL Azure

Because of the remoteness of SQL Azure it is beneficial to have some tricks in your coding toolbox for dealing with large binary objects, the varbinary(max) data type in SQL Azure. One of these is to be able to stream large binary objects (BLOB) -- reading or writing a piece of the data at a time.

This article provides a SqlStream class written in C# code. The class implements the abstract Stream class for the varbinary(max) data type on SQL Azure; Stream is an abstract class defined in the .NET CLR that is well supported and very versatile. The SqlStream class provided when used with SQL Azure allows you to manipulate a single blob a chunk at a time.

Using the SqlStream class provided you can:

Create a console application to copy a file of BLOB data to SQL Azure from the command line without having to load the whole BLOB into memory.

Create a Windows Azure Role that would read a BLOB from SQL Azure and store it in Windows Azure Storage.

Stream the BLOB from SQL Azure to a Winform application one chunk at a time - with the added benefit of being able to provide a good status dialog with progress bar.

Use the BinaryWriter, BinaryReader, FileStream, and MemoryStream classes in CLR to read and write the varbinary(max) data type without having to load the all the data into memory.

The blog post is a start of a series, in the coming days we will build some of the applications listed above. For now, here are some samples for using the SqlStream class. Download the SqlStream class its own .cs file at the bottom of the post.

Samples

The first sample uses the SqlStream class to read an image from the Adventure Works database deployed on SQL Azure and saving it to a file. You can download the Adventure Works database from SQL Server Database Samples.