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>Adding a row to the tableHaving completed the object relational mapping, it is quite easy to perform operationsthat manipulate the data stored in the associated tables. <strong>The</strong> C# code in Listing 4-4 adds anew record into the Items table. One <strong>of</strong> the columns being populated is a <strong>FILESTREAM</strong>column. <strong>The</strong> content <strong>of</strong> a disk file is read and copied into the ItemImage property <strong>of</strong> theEF class Item, which represents the <strong>FILESTREAM</strong> column in the table.static void Main(string[] args){Item itm = new Item();itm.ItemNumber = "IT001";itm.ItemDescription = "Micros<strong>of</strong>t Mouse";itm.ItemImage =System.IO.File.ReadAllBytes("c:\\temp\\micros<strong>of</strong>tmouse.jpg");NorthPoleEntities npe = new NorthPoleEntities();npe.AddObject("Items", itm);try{npe.SaveChanges();}catch (Exception ex){Console.Write(ex.Message);}}npe.Dispose();Listing 4-4:Adding a new record to the Items table using EF.Note that there is nothing <strong>FILESTREAM</strong>-specific in this code. Though the column beingpopulated is a <strong>FILESTREAM</strong> column, the code treats it as a VARBINARY(MAX) column.142

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

Saved successfully!

Ooh no, something went wrong!