title: C# - Why is MySQL Server slow in my server machine?
body: <p>I developed a POS like application and during testing with 2 PCs I didn't encounter any problems with the speed. It's just a simple LAN cable setup between 2 computers. But when I deployed it in a client, it ran slow.</p>
  
  <p>The client has 1 PC serving as the admin and the main server, and there are 2 more PCs serving as the cashier. All connected in a router. The cashiers are connected to the admin's PC (main server) to retrieve, insert, update and delete data. I just want to ask if there are processes that needs to be done in MySQL or are there anything wrong with my codes when connecting to the database.</p>
  
  <p>Here's my sample code for connecting to the database, I doubt having problems with it as this has been the standard in connecting to a database and adding records. Just in case I might bore you with codes, you can simply jump to the second code I posted, I have a comment there asking if the initialization of my class is correct. Thanks everyone!</p>
  
  <pre><code>class DBConnection
  {
      private MySqlConnection connection;
      private MySqlCommand cmd;
      private MySqlDataReader dr;
      private DataTable tbl;
      private MySqlDataAdapter da;
      private DataSet ds;
      private string connectionString;
      private string server;
      private string database;
      private string uid;
      private string password;
  
      private frmNotifOk myNotification;
      public DBConnection()
      {
          Initialize();
      }
      private void Initialize()
      {
          server = "CASHIER";
          database = "sampledb";
          uid = "root";
          password = "samplepassword";
          connectionString = "SERVER=" + server + ";" + "DATABASE=" + database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";
          connection = new MySqlConnection(connectionString);
      }
  
      private bool OpenConnection()
      {
      try
      {
          connection.Open();
          return true;
      }
      catch (MySqlException ex)
      {
          switch (ex.Number)
          {
              case 0:
                  MessageBox.Show("Cannot connect to server.");
                  break;
          }
          return false;
      }
  }
  
  private void CloseConnection()
  {
      try
      {
          connection.Close();
      }
      catch (MySqlException ex)
      {
          MessageBox.Show("Error: " + ex.Message);
      }
  }
  
  public void AddRecord(String DBQuery, bool showNotif)
  {
      string query = DBQuery;
      bool notify = showNotif;
      try
      {
          if (this.OpenConnection() == true)
          {
              cmd = new MySqlCommand(query, connection);
              cmd.ExecuteNonQuery();
              if (notify)
              {
                  MessageBox.Show("Item successfully added.");
              }
          }
      }
      catch (Exception ex)
      {
          MessageBox.Show("Error: " + ex.Message);
      }
      finally
      {
          this.CloseConnection();
      }
  }
  </code></pre>
  
  <p>And finally, here's how I use the method in a form:</p>
  
  <pre><code>public partial class frmNewCashier : Form
  {
      private DBConnection dbConnect;
      string sampleDataSource= "SELECT * FROM SampleTable";
  
      public frmNewCashier()
      {
          InitializeComponent();
          //Is this the correct place of initializing my DBConnection class?
          dbConnect = new DBConnection();
      }
  
      private void frmCashier_Load(object sender, EventArgs e)
      {
          try
          {
              dgvSearchItems.DataSource = dbConnect.DatabaseToDatagrid(dgvSearchItemsDataSource);
          }
          catch (Exception ex)
          {
              MessageBox.Show("Error: " + ex.Message);
          }
      }
  
  }
  </code></pre>
  
  <p>I put the initialization of DBConnection class in <code>public frmNewCashier()</code>, is this the correct place or should I put it in Load event or somewhere? I'm thinking if this has bearing to the slowness of database. Aside from this question, do you know anything that I might have missed that causes the slowness?</p>