title: C# method returns connect to different databases
body: <p><strong>UPD:</strong>
  I have a static class for work with database. </p>
  
  <p>This class contains the method which returns connect to database. Early this method returns connect to Advantage database (AdsConnection):</p>
  
  <pre><code>static private AdsConnection GetConnection(){
      var conn = new AdsConnection();
      conn.ConnectionString = here my connection string
      return conn;
  }
  </code></pre>
  
  <p>Now, I need to change this method. I need that this method returns connect to different databases types (Advantage database, Oracle database).</p>
  
  <p>The method will work into public methods in my class. For example, method for get data from any table from database.</p>
  
  <pre><code>public static List&lt;entity1&gt; GetEntities(){}
  </code></pre>
  
  <p>Into this method the first of my step is to resolve the type of database, then  connect to database. Then get data from database and the last step is return data (List&lt; entity1 >).</p>
  
  <p>In the step to connect the database I need to use the method <code>GetConnection("Ads")</code></p>
  
  <p>This method returns current connect to database and then I can use this connect for work</p>
  
  <p>I changed method:</p>
  
  <p><strong>My first version</strong></p>
  
  <pre><code>static private T GetConnection&lt;T&gt;(string dbType)
          {
              if (dbType.Equals("Oracle"))
              {
                  OdbcConnection conn = new OdbcConnection
                  conn.ConnectionString = here my connection string
                  return (T)conn;
              }
  
              if (dbType.Equals("Ads"))
              {
                  AdsConnection conn = new AdsConnection
                  conn.ConnectionString = here my connection string
                  return (T)conn;
              }
  
              return default(T);
          }
  </code></pre>
  
  <p>But, my solution does not work. I have errors:</p>
  
  <ol>
  <li>Cannot convert type 'System.Data.Odbc.OdbcConnection' to 'T'</li>
  <li>Cannot convert type 'Advantage.Data.Provider.AdsConnection' to 'T'
  I do not know how to resolve my problem.</li>
  </ol>
  
  <p>Please, tell me how to resolve my problem?</p>
  
  <p><strong>Now, I use the following code (this solution give me @khlr):</strong></p>
  
  <pre><code>static private IDbConnection GetConnection(string dbType)
  {
      if (dbType.Equals("Oracle"))
      {
          OdbcConnection conn = new OdbcConnection
          conn.ConnectionString = here my connection string
          return conn;
      }
  
      if (dbType.Equals("Ads"))
      {
          AdsConnection conn = new AdsConnection
          conn.ConnectionString = here my connection string
          return conn;
      }
  
      return null;
  }
  </code></pre>
  
  <p>Thank.</p>