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 6: <strong>FILESTREAM</strong> with SSIS and SSRSSo far, we have obtained a Connection object pointing to the target database, andidentified the ID <strong>of</strong> the item to be processed.<strong>The</strong> approach presented in this lab does a round trip to the server at each iteration <strong>of</strong> theloop. This is to keep the duration <strong>of</strong> <strong>FILESTREAM</strong> transactions minimal. Remember thatall access to the <strong>FILESTREAM</strong> data should be made within the scope <strong>of</strong> a transaction.An alternative approach would be to start a transaction at the beginning <strong>of</strong> the processand fetch the PathName() <strong>of</strong> all the rows with a single database call. In this case, theadditional round trips to the server can be avoided. However, this approach will keep atop level transaction open until the process completes, which could block the requestsfrom other connections.We now need a few local variables to store the <strong>FILESTREAM</strong> transaction context, thelogical path name <strong>of</strong> the <strong>FILESTREAM</strong> data value, and the path to the disk file that wewant to load into the <strong>FILESTREAM</strong> column, as shown in Listing 6-12.byte[] FSContext = null;string FSPathName = string.Empty;string FileName = string.Empty;Listing 6-12:Setting up the local variables.Next, we'll start a database transaction and execute the stored procedure (Listing 6-13).SqlCommand cmd = new SqlCommand();cmd.Connection = con;cmd.CommandText = "GetItemInfo";cmd.CommandType = CommandType.StoredProcedure;cmd.Parameters.AddWithValue("@ItemID", ItemID);SqlTransaction transaction = con.BeginTransaction("ItemTran");cmd.Transaction = transaction;SqlDataReader reader = cmd.ExecuteReader();Listing 6-13:Starting a database transaction and executing the stored procedure.251

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

Saved successfully!

Ooh no, something went wrong!