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 5: <strong>FILESTREAM</strong> with ASP.NET and Silverlight<strong>The</strong> web page expects a parameter in the query string named ItemNumber (Listing 5-11).protected void Page_Load(object sender, EventArgs e){string ItemNumber;ItemNumber = Request.QueryString["ItemNumber"].ToString();//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 = "GetItemImageInfo";cmd.Connection = cn;cmd.CommandType = System.Data.CommandType.StoredProcedure;cmd.Parameters.AddWithValue("@ItemNumber", ItemNumber);//Begin a transactionSqlTransaction trn = cn.BeginTransaction("ItemTran");cmd.Transaction = trn;string filePath = null;byte[] txContext = null;try{//Execute the commandSqlDataReader reader = cmd.ExecuteReader();if (reader.Read()){txContext = (reader["txContext"] as byte[]);filePath = reader["filePath"].ToString();}reader.Close();// Open the <strong>FILESTREAM</strong> data file for readingSqlFileStream fs = new SqlFileStream(filePath, txContext,FileAccess.Read);191

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

Saved successfully!

Ooh no, something went wrong!