MySQL integration with C # (Windows Forms) does not work?

advertisements

I am making a Windows Forms application using C# I have a running MySQL db with the help of WAMP. I have referenced the MySQL dll's in my project. I am not receiving any errors from the console or within the code itself. My problem, however, is that my SQL-command does not work and I have no idea why...

Here is the code:

private void ConnectToDB(String title, String content, String price)
    {
        db_connection();
        MySqlCommand cmd = new MySqlCommand();
        cmd.CommandText = "INSERT INTO testing.customers (itemTitle, itemContent, itemPrice) VALUES('" + title + "', '" + content + "', '" + price + "');";
        cmd.Connection = connect;
        connect.Close();
    }

private void db_connection()
    {
        try
        {
            conn = "Server=localhost;Database=testinbg;Uid=TheAdmin;Pwd=dsa67as7d66asd;";
            connect = new MySqlConnection(conn);
            connect.Open();
        } catch (Exception e)
        {
            MessageBox.Show(e.ToString());
        }
    }

I have given the user 'TheAdmin' all privileges? Am I missing something? :)


You have to execute your query:

  cmd.EndExecuteNonQuery();

The implementation

  conn = "Server=localhost;Database=testinbg;Uid=TheAdmin;Pwd=dsa67as7d66asd;";
  ...
  // Wrap IDisposable (i.e. MySqlConnection) into "using"
  using (var connect = new MySqlConnection(conn))
  {
    connect.Open();
    using (MySqlCommand cmd = new MySqlCommand())
    {
      cmd.Connection = connect;
      cmd.CommandText = @"INSERT INTO...";
      cmd.EndExecuteNonQuery();
    }
  }