title: How to retrieve file from database?
body: <p>I have saved a file in a database by converting it in bytes. It is saved successfully but I am unable to retrieve it.</p>
  
  <p>Here is code which I used to convert and save:</p>
  
  <pre><code>public static void databaseFilePut(string varFilePath)
  {
      byte[] file;
  
      using (var stream = new FileStream(varFilePath, FileMode.Open, FileAccess.Read))
      {
          using (var reader = new BinaryReader(stream))
          {
              file = reader.ReadBytes((int)stream.Length);
          }
      }
  
      using (SqlConnection varConnection = new SqlConnection("Data Source=FARHANB;Initial Catalog=save;Integrated Security=True"))
      using (var sqlWrite = new SqlCommand("INSERT INTO Raporty (RaportPlik) Values(@File)", varConnection))
      {
          varConnection.Open();
          sqlWrite.Parameters.Add("@File", SqlDbType.VarBinary, file.Length).Value = file;
  
          sqlWrite.ExecuteNonQuery();
          varConnection.Close();
      }
  }
  </code></pre>
  
  <p>I am using this code to retrieve it from the database:</p>
  
  <pre><code>public static void databaseFileRead(string varID, string varPathToNewLocation)
  {
      using (SqlConnection varConnection = new SqlConnection("Data Source=FARHANB;Initial Catalog=save;Integrated Security=True"))
      using (var sqlQuery = new SqlCommand(@"SELECT [RaportPlik] FROM [dbo].[Raporty] WHERE [RaportID] = @varID", varConnection))
      {
          varConnection.Open();
          sqlQuery.Parameters.AddWithValue("@varID", varID);
  
          using (var sqlQueryResult = sqlQuery.ExecuteReader())
              if (sqlQueryResult != null)
              {
                  sqlQueryResult.Read();
                  var blob = new Byte[(sqlQueryResult.GetBytes(0, 0, null, 0, int.MaxValue))];
                  sqlQueryResult.GetBytes(0, 0, blob, 0, blob.Length);
  
                  using (var fs = new FileStream(varPathToNewLocation, FileMode.Create, FileAccess.Write))
                      fs.Write(blob, 0, blob.Length);
  
                  varConnection.Close();
              }
          }
      }
  </code></pre>
  
  <p>OR</p>
  
  <pre><code>public static void databaseFileRead(string varID, string varPathToNewLocation)
  {
      using (SqlConnection varConnection = new SqlConnection("Data Source=FARHANB;Initial Catalog=save;Integrated Security=True"))
      using (var sqlQuery = new SqlCommand(@"SELECT [RaportPlik] FROM [dbo].[Raporty] WHERE [RaportID] = @varID", varConnection))
      {
          varConnection.Open();
          sqlQuery.Parameters.AddWithValue("@varID", varID);
  
          using (var sqlQueryResult = sqlQuery.ExecuteReader())
              if (sqlQueryResult != null)
              {
                  sqlQueryResult.Read();
                  var blob = new Byte[(sqlQueryResult.GetBytes(0, 0, null, 0, int.MaxValue))];
                  sqlQueryResult.GetBytes(0, 0, blob, 0, blob.Length);
  
                  using (var fs = new FileStream(varPathToNewLocation, FileMode.Create, FileAccess.Write)) 
                      fs.Write(blob, 0, blob.Length);
              }
              varConnection.Close();
      }
  }
  </code></pre>
  
  <p>I have used both code blocks one by one but none of them is working.</p>
  
  <p><a href="https://stackoverflow.com/questions/2579373/saving-any-file-to-in-the-database-just-convert-it-to-a-byte-array">Link One(Original)</a>
  <a href="https://stackoverflow.com/questions/2445326/how-to-store-the-file-in-the-database">Link Two(Modification)</a></p>
  
  <p>First of all, I got the problem in </p>
  
  <pre><code>using (var varConnection = Locale.sqlConnectOneTime(Locale.sqlDataConnectionDetails))
  </code></pre>
  
  <p>So I changed it into <code>sqlconnection</code>. It's saving the bytes successfully so I don't think problem is here.</p>
  
  <p>Second problem I got was "Access denied". I was getting access denied error so I solved it by saving the file in the bin project.</p>
  
  <p>Now there is no error but there is also no file in the specified folder.</p>
  
  <p>Here are the references I passed in the method.</p>
  
  <pre><code>databaseFileRead("1",@"\here");
  </code></pre>
  
  <p>What should I do? Please guide me I'm new to the databases.</p>
  
  <p>Thank you</p>