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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 4: <strong>FILESTREAM</strong> with Entity Framework and LINQ to <strong>SQL</strong>Retrieving information from the databaseNow, let us see how to retrieve data from <strong>FILESTREAM</strong> columns using EF. Just as wesaw in the previous example, there is no <strong>FILESTREAM</strong>-specific code involved in this stepeither. EF treats <strong>FILESTREAM</strong> columns as VARBINARY(MAX) columns, and thereforethe code that we will write will be the same for <strong>FILESTREAM</strong> and VARBINARY(MAX)columns.<strong>The</strong> code in Listing 4-5 searches for a record with ItemNumber IT001, and returns anItem object that contains the information about the given record. <strong>The</strong> last part <strong>of</strong> thecode reads the <strong>FILESTREAM</strong> value stored in the ItemImage column and writes to a diskfile. Add the code to the Main() method in the Program class. You must also import theSystem.Data.Objects namespace at the top <strong>of</strong> your code file.NorthPoleEntities npe = new NorthPoleEntities();foreach (Item itm in npe.Items.Where("it.ItemNumber == IT001")){System.IO.File.WriteAllBytes("c:\\temp\\mm.jpg", itm.ItemImage);}npe.Dispose();Listing 4-5:Retrieving BLOB values stored in a <strong>FILESTREAM</strong> column.That completes Lab 2, in which we created a very simple console application thatdemonstrated how to read and write <strong>FILESTREAM</strong> values using EF. Run theapplication, or debug and step through the code, to see our code in action.143

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

Saved successfully!

Ooh no, something went wrong!