title: Open/Close a database from a stream
body: <p>I want to access a SQLite Database from a given Stream (FileStream/Memorystream)?</p>
  
  <p>This is my code so far (it reads the File into a Memorystream and then opens it in SQLite):</p>
  
  <pre><code>if (File.Exists(path_DB))
  {
      byte[] file = File.ReadAllBytes(path_DB);
      MemoryStream mem = new MemoryStream(file);
  
      using (SQLiteConnection destination = new SQLiteConnection("Data Source=" + mem + ";Version=3;"))
      {
          destination.Open();
  
          using (SQLiteCommand command = new SQLiteCommand())
          {
              command.Connection = destination;
              command.CommandText = "SELECT Name FROM MyTable;";
  
              using (SQLiteDataReader reader = command.ExecuteReader())
              {
                  while (reader.Read())
                  {
                      MessageBox.Show(reader["Name"].ToString());
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>This code triggers me</p>
  
  <blockquote>
    <p>SQL Logic error or missing Database, no such table: MyTable</p>
  </blockquote>
  
  <p>at <code>command.ExecuteReader()</code>. The file referred to with <code>path_DB</code> does have the table.</p>
  
  <p>Where do I go wrong?</p>
  
  <p>//Edit: Changed</p>
  
  <pre><code>byte[] file = File.ReadAllBytes(path_DB);
  MemoryStream mem = new MemoryStream();
  </code></pre>
  
  <p>to</p>
  
  <pre><code>byte[] file = File.ReadAllBytes(path_DB);
  MemoryStream mem = new MemoryStream(file);
  </code></pre>
  
  <p>and further I can load an existing Database into the memory one and save the memory DB again to disk like this:</p>
  
  <pre><code>if (File.Exists(path_DB))
  {
      using (SQLiteConnection destination = new SQLiteConnection("Data Source=:memory:;Version=3;"))
      {
          SQLiteConnection source = new SQLiteConnection("Data Source=" + path_DB + ";Version=3;"); //;Password=" + textBox2.Text + "
          source.Open();
  
          destination.Open();
  
          source.BackupDatabase(destination, "main", "main", -1, null, 0);
          source.Close();
  
          using (SQLiteCommand command = new SQLiteCommand())
          {
              command.Connection = destination;
              command.CommandText = "SELECT Name FROM MyTable;";
  
              using (System.Data.SQLite.SQLiteDataReader reader = command.ExecuteReader())
              {
                  while (reader.Read())
                  {
                      MessageBox.Show(reader["Name"].ToString());
                  }
              }
          }
  
          source = new SQLiteConnection("Data Source=" + path_DB + ";Version=3;"); //;Password=" + textBox2.Text + "
          source.Open();
  
          // save memory db to file
          destination.BackupDatabase(source, "main", "main", -1, null, 0);
          source.Close();
      }
  }
  </code></pre>
  
  <p>So I figured, there must be also a way to do this with a direct (decrypted) stream!?</p>