title: Connecting to MySQL database with C#
body: <p>I am using:</p>
  
  <pre><code>        if (connection.State != ConnectionState.Open)
          {
              OpenConnection();
          }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>private bool OpenConnection()
  {
      try
      {
          connection.Open();
          return true;
      }
      catch (MySqlException ex)
      {
          //When handling errors, you can your application's response based 
          //on the error number.
          //The two most common error numbers when connecting are as follows:
          //0: Cannot connect to server.
          //1045: Invalid user name and/or password.
          switch (ex.Number)
          {
              case 0:
                  Console.WriteLine("Cannot connect to server.  Contact administrator");
                  Console.Read();
                  break;
  
              case 1045:
                  Console.WriteLine("Invalid username/password, please try again");
                  Console.Read();
                  break;
          }
          return false;
      }
  }
  </code></pre>
  
  <p>I always get <code>System.InvalidOperationException: The connection is already open</code>. it doesn't make sense because I check if it's open already.</p>
  
  <p>Thanks in advance.</p>