6/4/2008 1:31:57 AM

This sample works only with SQL Server 2008. It will not work with any version of SQL Server earlier than SQL Server 2008.

This sample shows how to use the FILESTREAM feature to send and receive data incrementally with SQLPutData and SQLGetData.

SQL Server samples and sample databases must be downloaded and installed before you can view or work with them. For more information, see Considerations for Installing SQL Server Samples and Sample Databases.

Scenario

For more information about the FILESTREAM feature, see FILESTREAM Support (ODBC) in SQL Server Books Online.

Languages

This sample uses Visual C++.

Prerequisites

Before running this sample, make sure the following software is installed:

  • SQL Server or SQL Server Express, including Database Engine.
    You can download SQL Server Express from the Microsoft Download Center.
  • The SQL Server Database Engine samples. These samples are included with SQL Server. You can download the latest version of the samples at the Microsoft SQL Server Developer Center.
  • .NET Framework SDK 2.0 (or later) or Microsoft Visual Studio 2005 (or later). You can obtain .NET Framework SDK free of charge. For more information, see Installing the .NET Framework Documentation.

Building the Sample

  • You must specify a server. In ODBC_filestream.cpp, change "MyServer" to a valid server name.
  • Make sure your INCLUDE environment variable includes the directory that contains sqlncli.h.
  • The sample requires you to execute the following stored procedure, which you can find in the sample's scripts directory:
    USE master
    GO
    
    -- enable file stream
    exec sp_configure 'filestream access level', 2
    GO
    
    -- create directory for filestream database
    EXEC sp_configure 'show advanced options', 1
    GO
    RECONFIGURE
    GO
    EXEC sp_configure 'xp_cmdshell', 1
    GO
    RECONFIGURE
    GO
    EXEC xp_cmdshell 'md c:\filestreamdemo'
    GO
    
    -- Drop the filestream demo database
    IF EXISTS (SELECT name FROM master..sysdatabases WHERE name = 'myfilestreamdb')
        DROP DATABASE [myfilestreamdb]
    GO
    
    -- Create filestream demo database
    CREATE DATABASE [myfilestreamdb] ON PRIMARY
        (NAME=[myfilestreamdbprimary], FILENAME='c:\filestreamdemo\dbf.mdf'),
        FILEGROUP [fsgrp] CONTAINS FILESTREAM (NAME=[fscnt],FILENAME='c:\filestreamdemo\fscnt')
        LOG ON (NAME=[dblog], FILENAME='c:\filestreamdemo\db1.ldf', SIZE=5MB, MAXSIZE=3000MB, FILEGROWTH=5MB)
    GO
    
    -- Create table that contain filestream column
    CREATE TABLE [myfilestreamdb]..[mydocs]
    (
        id UNIQUEIDENTIFIER ROWGUIDCOL NOT NULL UNIQUE,
        doc VARBINARY(MAX) FILESTREAM
    )
    GO
    In SQL Server Management Studio, load and execute the Scripts\setup.sql script or execute the following command in a Command Prompt window:
    sqlcmd -E -I -i Scripts\setup.sql
  • If you are using Visual Studio, load the ODBC_filestream.sln file and build it.
  • If you are using MSBuild.exe, invoke MSBuild.exe at a command prompt. Pass in the ODBC_filestream.sln file, as follows:
    MSBuild ODBC_filestream.sln

Running the Sample

  • From Visual Studio, invoke Start Without Debugging (CTRL+F5).
  • If you built with MSBuild.exe, invoke ODBC_filestream.exe.

Deleting the Sample Database

To delete the database in c:\filestreamdemo, you have to detach the database in SQL Server Management Studio, as follows:

sp_detach_db 'myfilestreamdb'
IF EXISTS (SELECT name FROM master..sysdatabases WHERE name = 'myfilestreamdb') DROP DATABASE [myfilestreamdb]

See Also

Concepts

Data Access Samples

Help and Information

Getting SQL Server 2008 Assistance