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 ApplicationsListing 3-11 opens a disk file and copies the contents <strong>of</strong> the file into a <strong>FILESTREAM</strong>column.//Get a handle to the <strong>FILESTREAM</strong> data using OpenSqlFileStream APIfsHandle = OpenSqlFilestream(dstFilePath, <strong>SQL</strong>_<strong>FILESTREAM</strong>_WRITE, 0, tkn, tknSize, 0);//Open the disk file to readfileHandle = CreateFile(TEXT("C:\\temp\\micros<strong>of</strong>tmouse2.jpg"), GENERIC_READ,FILE_SHARE_READ, NULL, OPEN_EXISTING,FILE_FLAG_SEQUENTIAL_SCAN, NULL);const int COPYBUFFERSIZE = 4096;DWORD bytesRead = 0;DWORD bytesWritten = 0;do{//Read from the disk fileReadFile(fileHandle, buffer, COPYBUFFERSIZE, &bytesRead, NULL);//Write to the <strong>FILESTREAM</strong> columnif (bytesRead > 0){WriteFile(fsHandle, buffer, bytesRead, &bytesWritten, NULL);}} while (bytesRead == COPYBUFFERSIZE);//close the disk file handle before closing the transactionif ( fileHandle != INVALID_HANDLE_VALUE ) CloseHandle(fileHandle);Listing 3-11:Using C++ to read from a disk file and write into a <strong>FILESTREAM</strong> column.100

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

Saved successfully!

Ooh no, something went wrong!