Storing BLOB Data
To store a BLOB, an application must call an remote BLOB store (RBS) client library API with the following information on the BLOB to be stored:
A connection to the database that the BLOB data will be stored in.
A collection ID.
Optional command options that specify additional information on how the BLOB will be stored.
The following steps describe how to store a BLOB in RBS. This example assumes that the SQL collection was created with the appropriate policy and configuration settings, and that the collection ID is stored by the application.
To store a BLOB |
|
Pulling BLOBs from a Stream
The simplest way to store BLOB data is to "pull" it in from an application stream. To accomplish this, the application must call the SqlRemoteBlob.WriteFromStream method with the specified stream as a parameter. Data from the specified stream will be read and stored in the BLOB Store.
Example
The following code example demonstrates how to store BLOB data by pulling it in from an application memory stream.
void RunStoreStreamPull(string connectionString)
{
//Create and open a new SqlConnection
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
// Create a memory stream to read the BLOB data from.
byte[] blobData = Encoding.Unicode.GetBytes("Remote Blob Store Sample Application Store Stream Pull Test Data");
using (MemoryStream dataStream = new MemoryStream(blobData))
{
// Create a blob context for RBS to operate within.
using (SqlRemoteBlobContext blobContext = new SqlRemoteBlobContext(conn))
{
// Create a new remote BLOB.
using (SqlRemoteBlob blob = blobContext.CreateNewBlob())
{
// Rather than treating the blob as a Stream, we call a method to 'Pull' the contents
// of the dataStream into the blob stream.
// This method cannot be combined with calls to the Write() method on the same object.
blob.WriteFromStream(dataStream);
// Register the blob with the RBS auxiliary tables
// and insert the blob into the application's metadata
// tables. This registration happens automatically
// when SqlRemoteBlob.GetBlobId is called.
Console.WriteLine("Created Blob ID: {0}", BitConverter.ToString(blob.GetBlobId()));
// Add a new row containing the blob ID to
// the database table.
string addBlobEntry = @"
insert into
ApplicationSampleTable (SearchTags, BlobId)
values
('Sample, RBS, Demo', @blobId)
";
using (SqlCommand cmd = new SqlCommand(addBlobEntry, conn))
{
cmd.Parameters.Add(new SqlParameter("@blobId", blob.GetBlobId()));
cmd.ExecuteNonQuery();
}
Console.WriteLine("Blob ID successfully stored in the application sample table.");
ts.Complete();
}
}
}
}
}
Pushing BLOBs by Returning a Stream
It is also possible to store BLOB data by "pushing" it from the application to a stream object provided by RBS. Data in the stream will be written to the BLOB store by RBS..
To store BLOB data by writing to a stream, follow these steps:
Declare the BLOB context for the operation.
Create a SqlRemoteBlob object on the specified BLOB context.
Write BLOB data from the stream to the SqlRemoteBlob using the Write method inherited from System.IO.Stream.
Commit the BLOB data written to SqlRemoteBlob when all data has been written.
Close the stream.
Example
The following code example demonstrates how write and commit blob data from a memory stream.
void RunStoreStreamPush(string connectionString)
{
// Create and open a new SqlConnection.
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
// Create a memory stream to read the BLOB data.
byte[] blobData = Encoding.Unicode.GetBytes("Remote Blob Store Sample Application Store Stream Push Test Data");
using (MemoryStream dataStream = new MemoryStream(blobData))
{
// Create a BLOB context for RBS to operate within.
using(SqlRemoteBlobContext blobContext = new SqlRemoteBlobContext(conn))
{
// Create a new remote BLOB.
using (SqlRemoteBlob blob = blobContext.CreateNewBlob())
{
// Push the contents of the data stream into
// the BLOB stream using SqlRemoteBlob members
// inherited from System.IO.Stream.
int bufferLength = 256;
byte[] buffer = new byte[bufferLength];
int bytesRead = dataStream.Read(buffer, 0, bufferLength);
while (bytesRead > 0)
{
blob.Write(buffer, 0, bytesRead);
bytesRead = dataStream.Read(buffer, 0, bufferLength);
}
// Commit the BLOB data to the stream.
blob.Commit();
// Register the blob with the RBS auxiliary tables
// and insert the blob into the application's metadata
// tables. This registration happens automatically
// when SqlRemoteBlob.GetBlobId is called.
Console.WriteLine("Created Blob ID: {0}", BitConverter.ToString(blob.GetBlobId()));
// Add a new row containing the blob ID to
// the database table.
string addBlobEntry = @"
insert into
ApplicationSampleTable (SearchTags, BlobId)
values
('Sample, RBS, Demo', @blobId)
";
using (SqlCommand cmd = new SqlCommand(addBlobEntry, conn))
{
cmd.Parameters.Add(new SqlParameter("@blobId", blob.GetBlobId()));
cmd.ExecuteNonQuery();
}
Console.WriteLine("Blob ID successfully stored in the application sample table.");
ts.Complete();
}
}
}
}
}
}