title: Upload File and Save to Database
body: <p>I am trying to upload a file from disk and then insert the file into a varbinary db column.</p>
  
  <p>I can't seem to figure out how to insert the binary file.</p>
  
  <p>I am using C# and Linq to Sql in a WPF application.</p>
  
  <p>Here is what I am trying so far! Any suggestions or advice would be appreciated.    </p>
  
  <pre><code>private void UploadFile()
          {
              DatabaseData.DataClassesDataContext context = new DatabaseData.DataClassesDataContext();
              {
              OpenFileDialog dlgOpen = new OpenFileDialog();
              dlgOpen.Title = "Select file";
  
              FileData fd = new FileData();
              if (dlgOpen.ShowDialog() ?? false)
              {
                  FileStream inStream = File.OpenRead(dlgOpen.FileName);
                  //FileStream outStream = File.OpenWrite(dlgOpen.FileName + ".xlsx");
                  int b;
  
                  while ((b = inStream.ReadByte()) &gt; -1)
                      // outStream.WriteByte((byte)b);
  
  
                      fd.FileId = Guid.NewGuid();
                      //fd.DataFile = inStream;//DataFile is the Varbinary column in the db
                      fd.Title = dlgOpen.FileName;
                      fd.FileExtension = txtExtension.text;
  
                      context.FileDatas.InsertOnSubmit(fd);
                      context.SubmitChanges();
  
                      //outStream.Flush();
                      //outStream.Close();
                      inStream.Close();
              }
              }
          }
  </code></pre>