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 11: FileTableCONVERT(VARCHAR(20), CONVERT(BIGINT, SUBSTRING(CONVERT(BINARY(16), NEWID()), 13, 4))) + '/'-- Insert a new record, specifying the new path_locatorINSERT INTO Documents (name, is_directory, path_locator)VALUES ('2013 Order Files', 1, @path_locator)Listing 11-22: Create a new subfolder using T-<strong>SQL</strong>.Some <strong>of</strong> the complexities in adding files and folders using T-<strong>SQL</strong>, specifically children <strong>of</strong>other folders, may lead many developers to choose to use the Windows APIs instead. <strong>The</strong>next section discusses using the standard .NET System.File.IO namespace classes toachieve the same results.Using .NET file I/O APIsIn this section, we will use the traditional .NET file I/O APIs to access FileTable data.First, we will need to determine the path to the files and folders that are stored inthe FileTable. <strong>The</strong>re is a new T-<strong>SQL</strong> function that allows us to get the UNC path <strong>of</strong> aFileTable's file_stream column: GetFileNamespacePath(). This function takestwo optional arguments; the first determines if a full path (which includes server name,share name and FileTable directory name) is returned or if a relative path is returned.<strong>The</strong> second argument determines the format in which the server name is returned, andis similar to the PathName() function found on a regular <strong>FILESTREAM</strong> column. (<strong>The</strong>PathName() function works on the file_stream column, but returns the path youwould use to create a SqlFileStream instance in the context <strong>of</strong> a transaction.)Listing 11-23 shows an ADO.NET code snippet to retrieve the full path <strong>of</strong> a file whosename and location in the hierarchy is known. <strong>The</strong>re are other ways to initially find thepath_locator <strong>of</strong> a file or folder, which can then be used to retrieve the UNC path.420

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

Saved successfully!

Ooh no, something went wrong!