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 Applications//Re-open the <strong>FILESTREAM</strong> data file and move to the end <strong>of</strong> filefs = new SqlFileStream(filePath, txContext, FileAccess.ReadWrite);fs.Seek(0, SeekOrigin.End);localFile = new FileStream("C:\\temp\\original.txt",FileMode.Open, FileAccess.Read);//Append the content <strong>of</strong> original.txt againlocalFile.CopyTo(fs, 4096);localFile.Close();fs.Close();Listing 3-23:Performing a partial update.We just performed a partial update on the <strong>FILESTREAM</strong> column. We will now verify thatthe information is stored as expected, by reading the contents <strong>of</strong> the <strong>FILESTREAM</strong> datafile and creating a new text file with the data. We can then open the text file and verifythat the partial update took place correctly (Listing 3-24).//Open the <strong>FILESTREAM</strong> data file for 'READ' and copy contents//into a disk filefs = new SqlFileStream(filePath, txContext, FileAccess.Read);localFile = new FileStream("C:\\temp\\copy.txt", FileMode.Create);fs.CopyTo(localFile, 4096);localFile.Close();fs.Close();Listing 3-24:Reading from the <strong>FILESTREAM</strong> data file and writing to a disk file.Let's now open the text file (copy.txt) and verify that it contains two copies <strong>of</strong> the textdata that was present in original.txt (Figure 3-2).113

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

Saved successfully!

Ooh no, something went wrong!