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 4: <strong>FILESTREAM</strong> with Entity Framework and LINQ to <strong>SQL</strong><strong>The</strong> stored procedure takes three arguments that are mapped directly to fields in theItems table. <strong>The</strong>re is no input parameter for the ItemImage field because we provide adefault dummy value <strong>of</strong> 0x (zero-length binary value). This ensures that a <strong>FILESTREAM</strong>data file will be created for the new record, but the file is a zero-length (empty) file.We also select the value <strong>of</strong> the SCOPE_IDENTITY() function as a new field with nameNewItemID. Because ItemID is an IDENTITY column, we will use the value <strong>of</strong> that fieldto update the ItemID scalar property value <strong>of</strong> the stored Item object instance with thevalue assigned by the database engine.<strong>The</strong> second new stored procedure, GetSqlFileStreamInfo (Listing 4-7), will return thevalues necessary to create a SqlFileStream object instance for the <strong>FILESTREAM</strong> data<strong>of</strong> a given item (identified by the ItemID parameter). <strong>The</strong> stored procedure will returna single row containing the path name <strong>of</strong> the <strong>FILESTREAM</strong> data file, the transactioncontext <strong>of</strong> a previously started transaction and the length <strong>of</strong> the <strong>FILESTREAM</strong> data file.CREATE PROCEDURE [dbo].[GetSqlFileStreamInfo]@ItemID intASBEGIN-- SET NOCOUNT ON added to prevent extra result sets from-- interfering with SELECT statements.SET NOCOUNT ON;END-- Insert statements for procedure hereSELECT I.ItemImage.PathName() AS FilePath,GET_<strong>FILESTREAM</strong>_TRANSACTION_CONTEXT() AS "Context",DATALENGTH(I.ItemImage) AS "Size"FROM Items IWHERE I.ItemID = @ItemIDListing 4-7:Creating a stored procedure to retrieve the SqlFileStream constructor values.145

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

Saved successfully!

Ooh no, something went wrong!