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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 3: Accessing <strong>FILESTREAM</strong> Data from Client ApplicationsSqlTransaction trn = con.BeginTransaction("ItemTran");cmd.Transaction = trn;SqlDataReader reader;reader = cmd.ExecuteReader();// Retrieve the FilePath() <strong>of</strong> the image fileif (reader.Read()){txContext = (reader["txContext"] as byte[]);filePath = reader["filePath"].ToString();}reader.Close();Listing 3-18:Retrieving the <strong>FILESTREAM</strong> logical path name and transaction context.We can now go ahead and open the <strong>FILESTREAM</strong> data file. <strong>The</strong> sample code inListing 3-19 opens a <strong>FILESTREAM</strong> data file for read operation.//Open the <strong>FILESTREAM</strong> data fileSqlFileStream fs = new SqlFileStream(filePath, txContext, FileAccess.Read);Listing 3-19:Opening a <strong>FILESTREAM</strong> data file for read operation.Next, we open the target file into which we will write the data being read from the<strong>FILESTREAM</strong> data file (Listing 3-20).//Create the target data fileFileStream localFile = new FileStream("C:\\temp\\Micros<strong>of</strong>tMouse2.jpg",FileMode.CreateNew);Listing 3-20:Creating a disk file to store the BLOB data.In Listing 3-21, we read from the <strong>FILESTREAM</strong> data file and write into the disk file we justcreated.110

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

Saved successfully!

Ooh no, something went wrong!