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// Begin a transactionSqlTransaction trn = con.BeginTransaction("ItemTran");cmd.Transaction = trn;// Execute the querySqlDataReader reader;reader = cmd.ExecuteReader();Listing 3-5:Using C# to retrieve the transaction context and logical path name <strong>of</strong>a <strong>FILESTREAM</strong> data value.Step 3: Opening the <strong>FILESTREAM</strong> data file<strong>The</strong> next step is to open the <strong>FILESTREAM</strong> data file, using the wrapper function/classprovided by <strong>SQL</strong> <strong>Server</strong>. Direct access to the physical files stored in the <strong>FILESTREAM</strong> datacontainer can corrupt the database, so it's always advisable to access the data throughthese streaming APIs..NET developers can use the SqlFileStream object to access the <strong>FILESTREAM</strong> data.SqlFileStream exposes <strong>SQL</strong> <strong>Server</strong> data that is stored with the <strong>FILESTREAM</strong> columnas a sequence <strong>of</strong> bytes. It resides within the namespace System.Data.SqlTypes andwithin the assembly System.Data.dll. You must have .NET Framework 3.5 SP 1 or laterto use SqlFileStream class.SqlFileStream is derived from System.IO.Stream. Once you have a validSqlFileStream object representing the desired <strong>FILESTREAM</strong> data file, you canuse the Read(), Write() or the other "stream" functions available within the class tomanipulate your <strong>FILESTREAM</strong> data.Listing 3-6 shows how to open a <strong>FILESTREAM</strong> data file for reading.95

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

Saved successfully!

Ooh no, something went wrong!