0% found this document useful (0 votes)
24 views3 pages

CRUD

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views3 pages

CRUD

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

namespace CRUDApp

{
using System;
using System.Data;
using MySql.Data.MySqlClient;
public partial class frmLogin : Form
{
private MySqlConnection connection;
private string connectionString = "Server=localhost;Database=crudbdd;User
ID=root;Password=root;";

public frmLogin()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)


{

private void label1_Click(object sender, EventArgs e)


{

private void label3_Click(object sender, EventArgs e)


{

private void btnConectar_Click(object sender, EventArgs e)


{
try
{
connection = new MySqlConnection(connectionString);
connection.Open();
MessageBox.Show("Conexión exitosa a la base de datos.");
}
catch (MySqlException ex)
{
MessageBox.Show($"Error al conectar: {ex.Message}");
}
}

private void btnCreate_Click(object sender, EventArgs e)


{
string usuario = tbxusurio.Text;
string contrasena = tbxContrasena.Text;

string query = "INSERT INTO usuarios (usuario, contrasena) VALUES


(@usuario, @contrasena)";

using (MySqlCommand cmd = new MySqlCommand(query, connection))


{
cmd.Parameters.AddWithValue("@usuario", usuario);
cmd.Parameters.AddWithValue("@contrasena", contrasena);

try
{
cmd.ExecuteNonQuery();
MessageBox.Show("Registro creado exitosamente.");
}
catch (MySqlException ex)
{
MessageBox.Show($"Error al crear el registro: {ex.Message}");
}
}
}

private void btnDesconectar_Click(object sender, EventArgs e)


{
if (connection != null)
{
connection.Close();
MessageBox.Show("Conexión cerrada.");
}
}

private void btnDelete_Click(object sender, EventArgs e)


{
int id;
if (!int.TryParse(tbxID.Text, out id))
{
MessageBox.Show("Por favor, ingrese un ID válido.");
return;
}

string query = "DELETE FROM usuarios WHERE id = @id";

using (MySqlCommand cmd = new MySqlCommand(query, connection))


{
cmd.Parameters.AddWithValue("@id", id);

try
{
cmd.ExecuteNonQuery();
MessageBox.Show("Registro eliminado exitosamente.");
}
catch (MySqlException ex)
{
MessageBox.Show($"Error al eliminar el registro:
{ex.Message}");
}
}
}

private void label4_Click(object sender, EventArgs e)


{

private void btnRead_Click(object sender, EventArgs e)


{
string query = "SELECT * FROM usuarios";

using (MySqlCommand cmd = new MySqlCommand(query, connection))


{
try
{
MySqlDataAdapter adapter = new MySqlDataAdapter(cmd);
DataTable dataTable = new DataTable();
adapter.Fill(dataTable);
dataGridView1.DataSource = dataTable;
}
catch (MySqlException ex)
{
MessageBox.Show($"Error al leer los datos: {ex.Message}");
}
}
}

private void btnUpdate_Click(object sender, EventArgs e)


{
int id;
if (!int.TryParse(tbxID.Text, out id))
{
MessageBox.Show("Por favor, ingrese un ID válido.");
return;
}

string usuario = tbxusurio.Text;


string contrasena = tbxContrasena.Text;

string query = "UPDATE usuarios SET usuario = @usuario, contrasena =


@contrasena WHERE id = @id";

using (MySqlCommand cmd = new MySqlCommand(query, connection))


{
cmd.Parameters.AddWithValue("@id", id);
cmd.Parameters.AddWithValue("@usuario", usuario);
cmd.Parameters.AddWithValue("@contrasena", contrasena);

try
{
cmd.ExecuteNonQuery();
MessageBox.Show("Registro actualizado exitosamente.");

}
catch (MySqlException ex)
{
MessageBox.Show($"Error al actualizar el registro:
{ex.Message}");
}
}
}
}
}

You might also like