Está en la página 1de 3

namespace WindowsFormsApplication3 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void buttonX1_Click(object sender, EventArgs

e) { try { MySqlConnection cnx = new MySqlConnection("server=localhost;database=unid;user id=root;password=03tomatsu"); MySqlCommand cmd = new MySqlCommand("INSERT INTO tabla1 values(@idAlumno,@Nombre,@Licenciatura)", cnx); cmd.Parameters.Add(new MySqlParameter("@idAlumno", textBoxX1.Text)); cmd.Parameters.Add(new MySqlParameter("@Nombre", textBoxX2.Text)); cmd.Parameters.Add(new MySqlParameter("@Licenciatura", textBoxX3.Text)); cnx.Open(); cmd.ExecuteNonQuery(); cnx.Close(); textBoxX1.Text = ""; textBoxX2.Text = ""; textBoxX3.Text = ""; textBoxX1.Focus(); Form3 x = new Form3(); x.labelX1.Text = "Guardado OK"; x.Show(); } catch(MySqlException) { Form2 x = new Form2(); x.labelX1.Text = "El registro ya existe"; x.Show(); } } private void buttonX2_Click(object sender, EventArgs e) { MySqlConnection cnx = new MySqlConnection("server=localhost;database=unid;user id=root;password=03tomatsu"); MySqlCommand cmd = new MySqlCommand("update tabla1 set Nombre=@Nombre,Licenciatura=@Licenciatura where idAlumno=@idAlumno", cnx); cmd.Parameters.Add(new MySqlParameter("@idAlumno", textBoxX1.Text)); cmd.Parameters.Add(new MySqlParameter("@Nombre", textBoxX2.Text)); cmd.Parameters.Add(new MySqlParameter("@Licenciatura", textBoxX3.Text)); cnx.Open(); cmd.ExecuteNonQuery(); cnx.Close();

textBoxX1.Text = ""; textBoxX2.Text = ""; textBoxX3.Text = ""; textBoxX1.Focus(); Form3 x = new Form3(); x.labelX1.Text = "Actualizado OK"; x.Show(); } private void buttonX4_Click(object sender, EventArgs e) { MySqlConnection cnx = new MySqlConnection("server=localhost;database=unid;user id=root;password=03tomatsu"); MySqlCommand cmd = new MySqlCommand("select * from Tabla1 where idAlumno=@idAlumno", cnx); cmd.Parameters.Add(new cmd.Parameters.Add(new cmd.Parameters.Add(new cnx.Open(); MySqlDataReader leer = if (leer.HasRows) { while(leer.Read()) { leer.Read(); textBoxX1.Text = "" + leer["idAlumno"]; textBoxX2.Text = "" + leer["Nombre"]; textBoxX3.Text = "" + leer["Licenciatura"]; } } cnx.Close(); } private void buttonX3_Click(object sender, EventArgs e) { MySqlConnection cnx = new MySqlConnection("server=localhost;database=unid;user id=root;password=03tomatsu"); MySqlCommand cmd = new MySqlCommand("delete from tabla1 where idAlumno=@idAlumno", cnx); cmd.Parameters.Add(new MySqlParameter("@idAlumno", textBoxX1.Text)); cnx.Open(); cmd.ExecuteNonQuery(); cnx.Close(); textBoxX1.Text = ""; textBoxX2.Text = ""; textBoxX3.Text = ""; textBoxX1.Focus(); Form3 x = new Form3(); x.labelX1.Text = "Eliminado OK"; MySqlParameter("@idAlumno", textBoxX1.Text)); MySqlParameter("@Nombre", textBoxX2.Text)); MySqlParameter("@Licenciatura", textBoxX3.Text)); cmd.ExecuteReader();

x.Show();

} } }

También podría gustarte