title: C# Declare different database-types
body: <p>We have currently four different servers. On each server there is a database with one table. They are all named the same and the columns of the table are also exact the same.</p>
  
  <p>I would like to choose a database based from a parameter.</p>
  
  <p>My first though is to create some if-statements and declare it within.
  Turns out this will be a scope-problem:</p>
  
  <p><strong>If-Else statement</strong></p>
  
  <pre><code>string db = "db1"; // some input to say which database I want
  
  if (db == "db1") {
    Database1 database = new Database1();
  } else if (db == "db2") {
    Database2 database = new Database2();
  } else if (db == "db3") {
    Database3 database = new Database3();
  } else if (db == "db4") {
    Database4 database = new Database4();
  }
  
  // This will be a scope problem. Variables that are declared within a scope will be lost once you get out.
  </code></pre>
  
  <p>Next thing I tried is using Enum with a switch. Unfortunately once you declared "database" you cannot change the type afterwards</p>
  
  <p><strong>Enum and switch</strong></p>
  
  <pre><code>public enum dataBases{
    None,
    db1,
    db2,
    db3,
    db4
  }
  
  dataBases dbases
  
  switch (dbases) {
    case dataBases.db1:
      Database1 database = new Database1();
      break;
    case dataBases.db2:
      Database2 database = new Database2();
      break;
    case dataBases.db3:
      Database3 database = new Database3();
      break;
    case dataBases.db4:
      Database4 database = new Database4();
      break;
  }
  </code></pre>
  
  <p>The last thing I tried is creating a null-variable, but this fails immediately without the if/else-story behind it:</p>
  
  <p><strong>Null variable</strong></p>
  
  <pre><code>var database = null;
  database = new Database1();
  </code></pre>
  
  <p>I'm working with C# / MVC for a couple of months, so I don't know all the in's and out's of this. Hopefully you can help me out.</p>
  
  <p>Note: I have search on many terms in StackOverflow, unfortunately there were no suitable results for me.</p>
  
  <p>Many thanks in advance!</p>