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 Applications// Open and read file using SqlFileStream Classstring filePath = reader.GetString(0);object objContext = reader.GetValue(1);byte[] txContext = (byte[])objContext;SqlFileStream fs = new SqlFileStream(filePath, txContext, FileAccess.Read);Listing 3-6:Using C# to open a <strong>FILESTREAM</strong> data file for read access.<strong>The</strong> parameter filePath is the logical path name <strong>of</strong> the <strong>FILESTREAM</strong> data file weobtained using the PathName() method call, and txContext is the <strong>FILESTREAM</strong>transaction context associated with the current transaction.<strong>The</strong> third parameter specifies the file access mode. It can be one <strong>of</strong> Read, Write, orReadWrite. Read opens the <strong>FILESTREAM</strong> data file for reading all the content. WhenWrite is specified, the SqlFileStream object will point to a zero-byte file and existingdata will be overwritten when the object is closed and transaction is committed. WhenReadWrite is used, the file is opened for both reading and writing, and the file pointeris placed at the beginning <strong>of</strong> the file. By using the Seek() method, you can move the filepointer to any location in the file to overwrite the existing content, or you can move itto the end <strong>of</strong> the file to append more data. See SqlFileStream Class Reference, later in thischapter, for a detailed discussion <strong>of</strong> the different constructors and member functions <strong>of</strong>the SqlFileStream class.Non .NET programming languages can use the <strong>FILESTREAM</strong> Win32 API to obtain aWindows file handle to the requested <strong>FILESTREAM</strong> data file, and use the file handleto read or write <strong>FILESTREAM</strong> data. <strong>SQL</strong> <strong>Server</strong> 2008, or later, Native Client exposes anew Win32 API, OpenSqlFilestream(), which returns a Windows file handle to therequested <strong>FILESTREAM</strong> data file. Just like the .NET example we saw earlier, you need topass the <strong>FILESTREAM</strong> transaction context and logical path to the desired <strong>FILESTREAM</strong>data value when calling this function, as shown in Listing 3-7.96

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

Saved successfully!

Ooh no, something went wrong!