title: Retrieve files from SQL Server database
body: <p>I am building a site where I have to use file upload control for attaching support logs / mails etc... I felt  that saving files in a database would be the better option. </p>
  
  <p>I am using the code below to upload files. However, I am not able to test it as I don't know how to retrieve files from a database. Can some one please help me on this? </p>
  
  <p>File type can be anything.</p>
  
  <p>Code:</p>
  
  <pre><code>FileUrl = "C:\\Attachments\\"+Path.GetFileName(UploadCtrl.NavigateUrl);
  FileStream fs = new FileStream(FileUrl, FileMode.Open, FileAccess.Read);
  BinaryReader br = new BinaryReader(fs);
  long numBytes = new FileInfo(FileUrl).Length;
  buff = br.ReadBytes(Convert.ToInt32(numBytes));
  SqlConnection conn = new SqlConnection(SQLSrc.ConnectionString);
  conn.Open();
  SqlCommand command = conn.CreateCommand();
  string InsertQueryText = "insert into Attachments values ('" + Path.GetFileName(FileUrl) + "','" + MIME(Path.GetExtension(Att_Overview_Link.NavigateUrl)) + "','" + buff + "');";
  command.CommandText = InsertQueryText;
  command.ExecuteNonQuery();
  </code></pre>
  
  <p>Here, MIME is the user defined function to get the MIME value of the specified file type. </p>
  
  <p>Frontend: C# ASP.NET and SQL Server as backend</p>