title: C# WPF MVVM change between multiple database connection types
body: <p>I'm prepering for my final exam with C# WPF using MVVM creating an query editor which needs to support multiple database motors, and wondering what's the best practice of having multiple database connection types to switch between in a view.</p>
  
  <p>This includes having etc. oracle, mssql, mysql connections.</p>
  
  <p>I thought of two scenarios to do this which is:</p>
  
  <p>A) Create a new instance of a database connection, where it creates a new view window to display so the user can work for that specifik connection.</p>
  
  <p>B) Make a global access list to switch between connections by written command. etc. 'change database to xxxx', for the current view they are displaying.</p>
  
  <p>What i'm searching for, is scenario B), so it's more flexible for the user.  I'm so far being guided to read about dependency injection and inheritance, where it delegating from abstract baseclass to resolve this.</p>
  
  <p>The second thing is how to access this list afterwards in the command field, find the name of a database based on the database name written, and change the connection type for the (this) current view they are displaying. But, this needs to be unique due we cannot hard-code the connection type in any viewModels.</p>
  
  <p>Currently i'm guided using DataServices, with MVVMLight nuget, where it's created one per connection type. Here i store the connection in one list:</p>
  
  <pre><code> public class MySqlService : IMySqlService
  {
      private List&lt;MySqlConnection&gt; Connections = new List&lt;MySqlConnection&gt;();
  
    public MySqlConnection AddConnection(string hostName, string userName, string userPassword, string dataBase)
      {
          var connectionString = $"Server={hostName};database={dataBase};user id={userName};password={userPassword};";
          var mySqlCon = new MySqlConnection(connectionString);
  
          if(mySqlCon.State == ConnectionState.Closed)
          {
              mySqlCon.Open();
              Connections.Add(mySqlCon);
              return mySqlCon;
          }
          else
          {
              return null;
          }           
      }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/SXHJ4.png" rel="nofollow noreferrer">Result case</a></p>