Clase 1
Código:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using System.Windows.Forms;
namespace Insert
{
public class Prueba
{
public void Consulta(string conexion, string consulta, ToolStripProgressBar barra,ToolStripStatusLabel etiketa)
{
MySqlConnection conn;
MySqlCommand com;
try
{
conn = new MySqlConnection(conexion);
conn.Open();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
try
{
com = new MySqlCommand(consulta, conn);
if (Convert.ToBoolean(com.ExecuteNonQuery()))
{
barra.Minimum = 0;
barra.Maximum = 100;
barra.Value = 100;
etiketa.Text = "Cargado";
MessageBox.Show("Se han Ingresado los datos");
}
else
{
etiketa.Text = "esperando";
MessageBox.Show("Error");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
finally
{
conn.Close();
}
}
}
}
y para usarlo agregas dos textbox y un boton para aceptar
en el evento click del boton agregas lo sig:
Código:
private void button1_Click(object sender, EventArgs e)
{
Prueba obj = new Prueba();
string nombre, apellido;
nombre = textBox1.Text;
apellido = textBox2.Text;
string conexion = "server=localhost;uid=root;pwd=12345;database=c#";
string query = "insert into Alumnos (nombre,apellido) values('"+nombre+"','"+apellido+"');";
obj.Consulta(conexion, query,toolStripProgressBar1,toolStripStatusLabel1);
}