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 6: <strong>FILESTREAM</strong> with SSIS and SSRS<strong>The</strong> stored procedure will return the three critical pieces <strong>of</strong> information that we needed.<strong>The</strong> code in Listing 6-14 reads these values and assigns them to the local variables wecreated earlier.reader.Read()FSContext = (reader["FSContext"] as byte[]);FSPathName = reader["FSPath"].ToString();FileName = reader["ImageFileName"].ToString();reader.Close();Listing 6-14:Reading the values returned by the stored procedure.Listing 6-15 opens the <strong>FILESTREAM</strong> data file for writing, and the image file for reading. Itthen reads from the disk file and writes into the <strong>FILESTREAM</strong> column.// Open the <strong>FILESTREAM</strong> data file for writingSqlFileStream fs = new SqlFileStream(FSPathName, FSContext,FileAccess.Write);// Open the disk file for readingFileStream localFile = new FileStream(FileName,FileMode.Open, FileAccess.Read);// Start transfering data from disk file to <strong>FILESTREAM</strong> data fileBinaryWriter bw = new BinaryWriter(fs);const int bufferSize = 4096;byte[] buffer = new byte[bufferSize];int bytes = localFile.Read(buffer, 0, bufferSize);while (bytes > 0){bw.Write(buffer, 0, bytes);bw.Flush();bytes = localFile.Read(buffer, 0, bufferSize);}Listing 6-15:Reading from the disk file and writing to the <strong>FILESTREAM</strong> column.252

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

Saved successfully!

Ooh no, something went wrong!