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 ApplicationsNote that <strong>FILESTREAM</strong> data can be accessed using the OpenSqlFilestream() functiononly if the application connects to <strong>SQL</strong> <strong>Server</strong> using Windows authentication.If OpenSqlFilestream()succeeds, it returns a valid handle to the <strong>FILESTREAM</strong> datafile associated with the logical path name passed into the function as the first parameter.If the function fails, it returns INVALID_HANDLE_VALUE. <strong>The</strong> handle value returnedby this function can be used to manipulate the <strong>FILESTREAM</strong> data associated withthe handle. <strong>The</strong> Win32 API functions below can be used on the handle returned byOpenSqlFilestream() function.• ReadFile() – Reads information from the <strong>FILESTREAM</strong> data file.• WriteFile() – Writes information into the <strong>FILESTREAM</strong> data file.• TransmitFile() – Used to transmit <strong>FILESTREAM</strong> data over a socket.• SetFilePointer() – Moves the file pointer to a desired location in the diskfile associated with the handle. <strong>The</strong> next read/write operation will start from thenew location.• SetEndOfFile() – Truncates the file at the current file pointer position.• FlushFileBuffers() – Writes the data in all the buffers associated with a file intothe disk.• CloseHandle() – Closes the <strong>FILESTREAM</strong> file handle. If the table has UPDATEtriggers, they will be fired when the <strong>FILESTREAM</strong> file handle is closed.Note that the <strong>FILESTREAM</strong> data handle cannot be used with any Win32 APIs other thanthose listed above. If you pass the handle to any other Win32 API function, you will get anERROR_ACCESS_DENIED error.<strong>The</strong> <strong>FILESTREAM</strong> file handle should be closed by calling CloseHandle() before the currenttransaction is committed or rolled back.129

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

Saved successfully!

Ooh no, something went wrong!