title: C# and MySQL - Method Returning a DataReader (or its contents)
body: <p>Throughout my program, I'm going to be repeatedly using the SELECT command (of MySQL). Each time, I have to establish the connection and stuff. I'm thinking of making a method that receives a SELECT command string and returns the argument's equivalent DataReader. I'm thinking this'll help me reduce the large chunk of code I have to produce each time. </p>
  
  <p>I want to use it in such a way that:</p>
  
  <pre><code>MySqlDataReader myReader = myObj.loadDataToReader("SELECT * FROM tblSample");
  </code></pre>
  
  <p>And then, I can manipulate <code>myReader</code> like a regular MySqlDataReader. However, I'm concerned about the fact that the connection must be open when using a datareader, and of course, the thing about it (the reader and the connection) being needed to be closed and all (a conventional security measure, I believe). I've been looking at things in the internet, and I can't seem to find a hint of how to do this.</p>
  
  <p>I'm trying and I have the following line of code:</p>
  
  <pre><code>public MySqlDataReader loadDataToReader(string selectCommand)
  {
      MySqlDataReader myReader = null;
      string myConnectionString = "Data Source = " + server + "; User = " + user + "; Port = 3306; Password = " + password + ";";
      string useDataBaseCommand = "USE " + dbName + ";";
      using (MySqlConnection myConnection = new MySqlConnection(myConnectionString))
      {
          using (MySqlCommand myCommand = new MySqlCommand(useDataBaseCommand + selectCommand, myConnection))
          {
              try
              {
                  myConnection.Open();
                  myReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
              }
              catch (Exception ex)
              {
                  myConnection.Close();
                  MessageBox.Show(ex.Message, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
              }
              return myReader;
          }
      }
  }
  </code></pre>
  
  <p>And in another class's method, I create an object to be able to use the method defined above, in such a way that it looks like this:</p>
  
  <pre><code>string selectTableCommand = "SELECT * FROM tblusers WHERE Username = \'" + txtID.Text + "\' AND Password = \'" + txtPassword.Text + "\';";
  MySQLOperations objSQLOperations = new MySQLOperations("localhost", "root", "mypass", "mydatabase");
  MySqlDataReader myDataReader = objSQLOperations.loadDataToReader(selectTableCommand);
  if (myDataReader.Read() &amp;&amp; txtPassword.Text.Equals(myDataReader["Password"].ToString()))
       { /* do something */ }
  else
       { /* do something */ }
  </code></pre>
  
  <p>However, I get a message saying "Invalid attempt to read when reader is closed!"
  How can I correct this and make it work? Or rather, is there a much more appropriate way to do this? And how am I going to make sure my connection/reader is closed after using them?</p>