예제 #1
0
 private void ButtonUserEdit_Click(object sender, EventArgs e)
 {
     if (cekValid())
     {
         return;
     }
     else
     {
         Database        db      = new Database();
         string          strconn = db.getString();
         MySqlConnection conn    = new MySqlConnection(strconn);
         conn.Open();
         try
         {
             string       query = "UPDATE user SET username = @username, password = @password, nama = @nama WHERE id_user = @id_user";
             MySqlCommand cmd   = new MySqlCommand(query, conn);
             cmd.Parameters.AddWithValue("@id_user", id_user);
             cmd.Parameters.AddWithValue("@username", txtBoxUserEdit.Text);
             cmd.Parameters.AddWithValue("@password", textBoxPasswordBaru.Text);
             cmd.Parameters.AddWithValue("@nama", txtBoxNamaLengkapUser.Text);
             //MessageBox.Show(query);
             int sukses = cmd.ExecuteNonQuery();
             if (sukses > 0)
             {
                 MessageBox.Show("Data berhasil diupdate", "Sukses");
                 frm2.getAllUser();
             }
             else
             {
                 MessageBox.Show("Data gagal diupdate", "Gagal");
             }
         }
         catch (MySqlException ex)
         {
             MessageBox.Show(ex.ToString());
         }
         conn.Close();
     }
 }
예제 #2
0
        private void tambahuser()
        {
            user u = new user();

            u.setUsername(txtBoxusername.Text);
            string username = u.getUsername();

            u.setPassword(txtBoxpassword.Text);
            string password = u.getPassword();

            u.setNama(txtBoxnama.Text);
            string nama = u.getNama();

            Database        db      = new Database();
            string          strconn = db.getString();
            MySqlConnection conn    = new MySqlConnection(strconn);

            conn.Open();
            string       query = "INSERT INTO user VALUES(NULL, @username, @password, @nama)";
            MySqlCommand cmd   = new MySqlCommand(query, conn);

            cmd.Parameters.AddWithValue("@username", username);
            cmd.Parameters.AddWithValue("@password", password);
            cmd.Parameters.AddWithValue("@nama", nama);

            int hasil = cmd.ExecuteNonQuery();

            if (hasil > 0)
            {
                MessageBox.Show("Data berhasil ditambah", "Sukses");
                frm2.getAllUser();
            }
            else
            {
                MessageBox.Show("Data gagal ditambah", "Gagal");
            }

            conn.Close();
        }