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: FileTableYou may also want to use the file I/O APIs to create new files and folders. First, you'llneed to obtain the UNC path <strong>of</strong> a valid folder in the FileTable namespace. Listing 11-25shows how to obtain the UNC path <strong>of</strong> the FileTable root folder. It uses the T-<strong>SQL</strong>FileTableRootPath() function to retrieve the UNC path. <strong>The</strong> argument to thefunction is the name <strong>of</strong> the FileTable. If this argument is not specified, the database'sroot directory is returned. This function is discussed in more detail in the section belowon Managing FileTables.string SqlText = "SELECT FileTableRootPath('Documents')";string RootPath = null;using (SqlConnection conn = new SqlConnection("server=.;database=NorthPole;integrated security=sspi;"))using (SqlCommand cmd = new SqlCommand(SqlText, conn)){conn.Open();RootPath = (string)cmd.ExecuteScalar();}Listing 11-25: Obtaining the root folder <strong>of</strong> a FileTable in .NET.Listing 11-26 then uses this root folder value to create a folder and a file inside the newfolder. In this code snippet, the System.IO namespace is not explicitly specified for theclass names, so make sure you have a using statement (or Imports in Visual Basic) atthe top <strong>of</strong> your code file before running this code.if (RootPath != null){DirectoryInfo di = Directory.CreateDirectory(RootPath +"\\Order Files");using (StreamWriter sw = File.CreateText(di.FullName +"\\ReadMe.txt")){sw.WriteLine("Hello, World!");}}Listing 11-26: Creating a folder and file in a FileTable using .NET file I/O APIs.422

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

Saved successfully!

Ooh no, something went wrong!