title: Open Binary File From Database C#
body: <p>I have PDF file data in a SQL Server database, in the column type <code>image</code> (bad previous db designer). What I need to do is read that binary data out to the client, so that they could download the PDF directly to their computer. </p>
  
  <p>So far, my code is as follows:</p>
  
  <pre><code>SqlConnection con = new SqlConnection();
  con.ConnectionString = "casIntranetConnectionString";
  
  SqlCommand com = new SqlCommand("SELECT [File], [FileName] FROM [com].[catalog1] WHERE [FileName] = @filename");
  com.Connection = con;
  com.Parameters.AddWithValue("filename", Request.QueryString["filename"]);
  
  con.Open();
  
  SqlDataReader reader = com.ExecuteReader();
  
  if (reader.Read())
  {
      Response.Clear();
      Response.AddHeader("Content-Type", "application/pdf");
      Response.AddHeader("Content-Disposition", "inline; filename=" + Request.QueryString["filename"] + ".pdf");
  }
  </code></pre>
  
  <p>I'm assuming I'm going to need the reader to read out the bytes, but that's where I don't really know what I do. Any suggestions?</p>
  
  <p>Thanks!</p>