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>Adding a row to the tableWe will create a new method in the Program.cs file to insert a new item in the database.<strong>The</strong> code for this method is shown in Listing 4-8. This code will go in the Program class.static int InsertItem(){Item NewItem = new Item();NewItem.ItemNumber = "IT002";NewItem.ItemGuid = System.Guid.NewGuid();Console.WriteLine("New Item GUID: {0}",NewItem.ItemGuid.ToString());NewItem.ItemDescription = "Micros<strong>of</strong>t Mouse";using (NorthPoleEntities e = new NorthPoleEntities()){bool OpenedConn = false;if (e.Connection.State != System.Data.ConnectionState.Open){e.Connection.Open();OpenedConn = true;}try{// Start a transaction explicitly to ensure adding the row and// creating the BLOB are part <strong>of</strong> the same transaction// System.Transactions.TransactionScope can be used if requiredusing (System.Data.Common.DbTransaction tran =e.Connection.BeginTransaction()){e.Items.AddObject(NewItem);// Because this uses a stored procedure (instead <strong>of</strong>// generated T-<strong>SQL</strong>)// this will create a <strong>FILESTREAM</strong> data file// If adding records using a stored proc is not desired,// then this can be set as a DEFAULTe.SaveChanges();152

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

Saved successfully!

Ooh no, something went wrong!