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 ApplicationsUnderstanding Streaming Access to<strong>FILESTREAM</strong> DataIn order to gain streaming access to the <strong>FILESTREAM</strong> data, <strong>SQL</strong> <strong>Server</strong> exposes amanaged object for the .NET developers and a Win32 API for the Windows programmers.Having instantiated an SqlFileStream object, representing the <strong>FILESTREAM</strong> datafile (in case <strong>of</strong> .NET), or opened a <strong>FILESTREAM</strong> data file (using the <strong>SQL</strong> <strong>Server</strong> Win32API), you can read or write to the <strong>FILESTREAM</strong> data file using the FILE manipulationfunctions provided by the client library or the .NET programming language.So how does this differ from the traditional model <strong>of</strong> storing the BLOB data in the filesystem and storing the path to the data file in a relational table? How does <strong>SQL</strong> <strong>Server</strong>maintain transactional consistency between the relational and the <strong>FILESTREAM</strong> data?Well, when using the <strong>FILESTREAM</strong> streaming APIs, you are not accessing the<strong>FILESTREAM</strong> data directly; <strong>SQL</strong> <strong>Server</strong> opens the file handle and passes it to you throughits own wrapper APIs. This allows <strong>SQL</strong> <strong>Server</strong> to keep total control over the operationsand maintain transactional consistency between the relational data in the tables and theBLOB data in the <strong>FILESTREAM</strong> store.Accessing <strong>FILESTREAM</strong> data through the streaming APIs involves the steps below,irrespective <strong>of</strong> the client library or programming language you use.1. Start a transaction – You need a transaction to gain streaming access to the<strong>FILESTREAM</strong> data.2. Retrieve the path name and transaction context – You need to obtain a logical pathname to the <strong>FILESTREAM</strong> data file and a pointer to the current transaction contextto access the <strong>FILESTREAM</strong> data.3. Open the <strong>FILESTREAM</strong> data file – Gain access to the <strong>FILESTREAM</strong> data file eitherusing the .NET wrapper class or the Win32 <strong>FILESTREAM</strong> API.90

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

Saved successfully!

Ooh no, something went wrong!