17.07.2015 Views

The Art of SQL Server FILESTREAM - Red Gate Software

The Art of SQL Server FILESTREAM - Red Gate Software

The Art of SQL Server FILESTREAM - Red Gate Software

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 3: Accessing <strong>FILESTREAM</strong> Data from Client ApplicationsLab 1: Reads, Writes and Partial UpdatesIt is time for us to write some fully functional sample code that reads and writes<strong>FILESTREAM</strong> data to perform the <strong>FILESTREAM</strong> operations below.1. Inserting a new record (with T-<strong>SQL</strong>) and storing <strong>FILESTREAM</strong> data in the column.2. Reading information from a <strong>FILESTREAM</strong> column.3. Performing partial updates on the BLOB data stored in a <strong>FILESTREAM</strong> column.In the following sections, we'll provide the sample code and step-by-step explanationsfor each <strong>of</strong> these operations in C# using the Managed API. <strong>The</strong> sample code for VB.NET(using the Managed API) and C++ (using the Win32 API) is supplied separately in thedownload. Though the syntax and code used in each <strong>of</strong> these programming languagesdiffer a little, the overall flow <strong>of</strong> the operations remains the same across all theseprogramming languages.Inserting a new record with <strong>FILESTREAM</strong> dataLet us see an example that reads the content <strong>of</strong> a disk file and stores it in the<strong>FILESTREAM</strong> data container. We will load the content <strong>of</strong> the Micros<strong>of</strong>t Mouse image filethat we used in Chapter 2.<strong>The</strong> first step is to create a connection to the desired target <strong>SQL</strong> <strong>Server</strong> 2008 instancethat hosts the database with the <strong>FILESTREAM</strong>-enabled table in which we want to storethe BLOB data, as shown in Listing 3-14.//References at the top <strong>of</strong> the fileusing System;using System.Data;using System.Windows.Forms;using System.Data.SqlClient;106

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!