Overview

Filestream enable SQL Server Database to store binary (VARBINARY(MAX)) data into NTFS file system with no impact to SQL DML (Data Manipulation Language). Filestream increases the performance and availability of the database because it take advantage of NT System cache for caching data instead of SQL Server buffer pool that will give extra memory to SQL Server query processing.

Note: Filestream is only available in SQL Server 2008 server

When to Use Filestream

  • When object being stored are averaging 1MB or more
  • Fast Read access is important
  • You are developing applications that use middle tier  for application logic

Configuring SQL Server 2008 for File Stream

  1. Open SQL Server Configuration Manager
  2. Select SQL Server Services node.
  3. Right click the SQL Server 2008 instance you want to configure and select Properties from the context menu. This will launch SQL Server Instance Properties window.

  4. Select the FILESTREAM tab.
  5. Check Enable FILESTREAM for Transact-SQL access and click Ok button.
  6. Open SQL Management Studio and create new query. Enter the script below and run the query. This will reconfigure SQL Server.
    EXEC sp_configure filestream_access_level, 2
    RECONFIGURE

Creating Filestream Enabled Database

  1. Create new Query in SQL Server Management Studio.
  2. In the query window, enter enter the scripts below and make sure the D:\SqlData\SQL2008_Data path exist but it does not require that MYDB_Filestream folder exist.
    CREATE DATABASE "MYDB"  
    ON
    PRIMARY ( NAME = MYDB1, FILENAME = 'D:\SQLData\SQL2008_Data\MYDB.mdf'),
    FILEGROUP MYDB_FILESTREAM CONTAINS FILESTREAM( NAME = MYDB_FILESTREAM,
    FILENAME = 'D:\SQLData\SQL2008_Data\MYDB_FileStream')
    LOG ON ( NAME = MYDB_LOG,
    FILENAME = 'D:\SQLData\SQL2008_Data\MYDB.ldf')

    go
  3. Click Execute button to run the script. This will create database with Filestream enabled and it will create MYDB_fileStream folder under the filestream path.

Creating Table that Uses Filestream in Storing Data

  1. Create new Query in SQL Server Management Studio.
  2. In the query window enter the script below
    create table "FileData" ( 
    "FileID" uniqueidentifier not null,
    "RowGuid" UNIQUEIDENTIFIER NOT NULL ROWGUIDCOL UNIQUE DEFAULT NEWID(),
    "BinData" VARBINARY(MAX) FILESTREAM NULL)

    go
  3. Click Execute button to run the script. This will create a table that uses filestream to store BLOB data.

The "RowGuid" UNIQUEIDENTIFIER NOT NULL ROWGUIDCOL UNIQUE DEFAULT NEWID() is very important in this table because Filestream requires a UNIQUE ROWGUIDCOL column else it will return error.

 

Operating system error -2147024891: "0x80070005(Access is denied.)"

This happens when you run the CREATE DATABASE with FILESTREAM (Step #2 above) and you don't have the hotfix for SQL Server 2008. This only happens in windows XP operating system. To fix the issue, download the hotfix @ http://support.microsoft.com/?id=978835 and install in your machine.

Reference:

http://technet.microsoft.com/en-us/library/bb895234.aspx