title: Dynamic Select Statement in C# to MySQL database?
body: <p>I have quite a number of questions lately about SQL statements in MySQL, but I none have fit my purpose.</p>
  
  <p>I have a class which is where my SQL statements will be saved so that I could use them as methods to keep my code neat. I have already done the Insert, Update, Delete, and Count statements, but I can't seem to find a solution for the SELECT statement</p>
  
  <p>Here are the codes for the other statements:</p>
  
  <pre><code>public void Insert(string query)
      {
          if (OpenConnection() == true)
          {
              MySqlCommand cmd = new MySqlCommand(query, mycon);
              cmd.ExecuteNonQuery();
              CloseConnnection();
          }
      }
  
      public void Update(string query)
      {
          if (OpenConnection() == true)
          {
              MySqlCommand cmd = new MySqlCommand();
              cmd.CommandText = query;
              cmd.Connection = mycon;
  
              cmd.ExecuteNonQuery();
              CloseConnnection();
          }
      }
  
      public void Delete(string query)
      {
          if (OpenConnection() == true)
          {
              MySqlCommand cmd = new MySqlCommand(query, mycon);
              cmd.ExecuteNonQuery();
              CloseConnnection();
          }
      }
  
      public int Count(string query)
      {
          int count = 0;
  
          if (OpenConnection() == true)
          {
              MySqlCommand cmd = new MySqlCommand(query, mycon);
  
              count = int.Parse(cmd.ExecuteScalar() + "");
  
              CloseConnnection();
  
              return count;
          }
  
          else
          {
              return count;
          }
      }
  </code></pre>
  
  <p>I am thinking of a SELECT method that would look something like this:</p>
  
  <pre><code>    public string[] Select(string query, int colnum)
      {
          if (OpenConnection() == true)
          {
              MySqlCommand cmd = new MySqlCommand(query, mycon);
              MySqlDataReader dr = new MySqlDataReader();
              string[] info = new string[colnum];
              dr = cmd.ExecuteReader();
              while (dr.Read())
              {
                  for (int i = 0; i &lt;= colnum; i++)
                  {
                    info[i] = (dr[colnum].ToString());
                  }
              }
          }
      }
  </code></pre>
  
  <p>this line is quite fictional <code>info[i] = (dr[colnum].ToString());
  </code> since I know It will not function the way I want it to. I am looking for something with a similar logic or something like that that would let me take advantage of the SELECT statements by using methods..</p>