title: What is a more efficient way to query MySQL using C#?
body: <p>Based on links around the StackOverflow site (references below), I've come up with this block of code to perform queries from my C# application to a MySQL database.</p>
  
  <pre><code>using (var dbConn = new MySqlConnection(config.DatabaseConnection))
  {
      using (var cmd = dbConn.CreateCommand())
      {
          dbConn.Open();
          cmd.CommandType = CommandType.Text;
          cmd.CommandText = "SELECT version() as Version";
  
          using (IDataReader reader = cmd.ExecuteReader())
          {
              if (reader.Read())
              {
                  Console.WriteLine("Database Version: " + reader.GetString(reader.GetOrdinal("Version")));
              }
          }
      }
  }
  </code></pre>
  
  <p>The problem I have with this, is that I have to build up this massive block of code every time I have a group of queries to make because I don't (and shouldn't) leave the connection open for the life of the application. </p>
  
  <p>Is there a more efficient way to build the supporting structure (the nested <code>using</code>s, opening the connection, etc), and instead pass my connection string and the query I want to run and get the results back?</p>
  
  <p>Referenced questions:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/10675102/correct-use-of-connections-with-c-sharp-and-mysql">Use of connections with C# and MySql</a> - Specifically the answer
  by tsells </li>
  <li><a href="https://stackoverflow.com/questions/9787097/mysql-select-where-and-c-sharp">Mysql select where and C#</a> </li>
  <li><a href="https://stackoverflow.com/questions/10323498/update-a-mysql-table-using-c-sharp">Update a mysql table using
  C#</a></li>
  </ul>
  
  <p>That is three of the ones I looked at. There were a few more, but my Google-fu can't refind them right now. All of these provide answers for how to perform a single query. I want to perform separate business logic queries - a few of them repeatedly - and don't want to repeat unneeded code.</p>
  
  <p>What I've tried:
  Based on the comment from nawfal, I have these two methods:</p>
  
  <pre><code>private MySqlDataReader RunSqlQuery(string query)
  {
      Dictionary&lt;string, string&gt; queryParms = new Dictionary&lt;string, string&gt;();
      MySqlDataReader QueryResult = RunSqlQuery(query, queryParms);
      return QueryResult;
  }
  
  private MySqlDataReader RunSqlQuery(string query, Dictionary&lt;string, string&gt; queryParms)
  {
      MySqlDataReader reader = null;
      if (queryParms.Count &gt; 0)
      {
          // Assign parameters
      }
  
      try
      {
          using (var dbConn = new MySqlConnection(config.DatabaseConnection))
          {
              using (var cmd = dbConn.CreateCommand())
              {
                  dbConn.Open();
                  cmd.CommandType = CommandType.Text;
                  cmd.CommandText = query;
  
                  using (reader = cmd.ExecuteReader())
                  {
                      return reader;
                  }
              }
          }
      }
      catch (MySqlException ex)
      {
          // Oops.
      }
  
      return reader;
  }
  </code></pre>
  
  <p>The problem with this attempt is that the <code>reader</code> closes when it is returned from the method. </p>