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

  1. Establish an active SQL connection to the database in which the BLOB will be stored and the RBS context.

  2. Determine the collection ID value of the collection in which the blob will be stored. Collections allow applications to manage different groups of BLOBs inside a single database.

  3. Create a SqlRemoteBlob object that represents the BLOB data on the BLOB context by calling CreateNewBlob. You may configure the SqlRemoteBlob by specifying command options, such as setting the BlobStoreName, in an optional ConfigItemList parameter.

  4. Store the BLOB data in the BLOB store by pushing or pulling the BLOB data. The following sections describe how to perform these operations in greater detail.

  5. Store the BlobId property of the SqlRemoteBlob object in the RBS column of the application table.

  6. Dispose the SqlRemoteBlob object.

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:

  1. Declare the BLOB context for the operation.

  2. Create a SqlRemoteBlob object on the specified BLOB context.

  3. Write BLOB data from the stream to the SqlRemoteBlob using the Write method inherited from System.IO.Stream.

  4. Commit the BLOB data written to SqlRemoteBlob when all data has been written.

  5. 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();
                        }
                    }
                }
            }
        }
    }