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 5: <strong>FILESTREAM</strong> with ASP.NET and SilverlightNote that this stored procedure inserts a dummy value into the <strong>FILESTREAM</strong> columnbecause, as we have already seen in Chapter 3, Win32/Managed API access to a<strong>FILESTREAM</strong> column is possible only if the column is non-NULL.<strong>The</strong> stored procedure returns a result set that contains the PathName(), the logicalidentifier pointing to the disk file associated with the <strong>FILESTREAM</strong> column in the newlyinserted row, and a <strong>FILESTREAM</strong> transaction context <strong>of</strong> the currently active transaction.This is done to avoid an additional database call when writing the code to save the<strong>FILESTREAM</strong> data. For more information, refer back to Chapter 3.Saving the <strong>FILESTREAM</strong> dataAfter the relational data is saved, we will execute the code to save the <strong>FILESTREAM</strong> datausing the <strong>FILESTREAM</strong> Managed API. We saw in Chapter 3 an example <strong>of</strong> how to load thecontent <strong>of</strong> a disk file into the <strong>FILESTREAM</strong> column. In this case, we will obtain a streampointing to the file selected by the user through the FileContent property <strong>of</strong> the fileupload control (Listing 5-8).private void SaveItem(){//Create and open a database connectionSqlConnection cn = new SqlConnection();cn.ConnectionString =System.Configuration.ConfigurationManager.ConnectionStrings["NorthPoleDB"].ToString();Cn.Open();//Create a Command objectSqlCommand cmd = new SqlCommand();cmd.CommandText = "SaveItemImage";cmd.Connection = cn;cmd.CommandType = CommandType.StoredProcedure;cmd.Parameters.AddWithValue("@ItemNumber", txtItemNumber.Text);cmd.Parameters.AddWithValue("@ItemDescription",txtItemDescription.Text);173

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

Saved successfully!

Ooh no, something went wrong!