
16/07/2009, 11:56
|
| | Fecha de Ingreso: mayo-2007 Ubicación: PIC-16F84A
Mensajes: 729
Antigüedad: 17 años, 8 meses Puntos: 8 | |
Respuesta: Problemas con C# Te dejo el código.
Código:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient; // No olvidar este using.
using MySql.Data.Types; // No olvidar este using.
namespace MySQL12
{
public partial class Form1 : Form
{
MySqlConnection connection = null; // No olvidar.
string MyConString;
public Form1()
{
InitializeComponent();
}
private void button_crear_bd_Click(object sender, EventArgs e)
{
#region Bases de datos.
string MyConString = "SERVER=" + textBox_Servidor.Text + ";" +
"UID=" + textBox_Usuario.Text + ";" +
"PASSWORD=" + textBox_Contraseña.Text + ";";
// Aquí va 'pic_remoto'.
string bd_pic_remoto = "CREATE DATABASE IF NOT EXISTS `" + textBox_Base_de_datos.Text + @"`DEFAULT CHARACTER SET latin1 COLLATE latin1_spanish_ci;
USE `" + textBox_Base_de_datos.Text + "`;";
connection = new MySqlConnection(MyConString);
MySqlCommand con = new MySqlCommand(bd_pic_remoto, connection);
connection.Open();
con.ExecuteNonQuery();
connection.Close();
// Estructura de tabla para la tabla `dispositivos`
string tb_dispositivos = "CREATE TABLE IF NOT EXISTS `" + textBox_dispositivo.Text + @"` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`nombre` varchar(6) COLLATE latin1_spanish_ci NOT NULL,
`estado` tinyint(2) NOT NULL,
`descripcion` varchar(20) COLLATE latin1_spanish_ci NOT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_spanish_ci AUTO_INCREMENT=5;";
connection = new MySqlConnection(MyConString);
MySqlCommand con2 = new MySqlCommand(tb_dispositivos, connection);
connection.Open();
con2.ExecuteNonQuery();
connection.Close();
// (Evento) desencadenante `dispositivos`
string drop_trigger = "DROP TRIGGER IF EXISTS `" + textBox_Base_de_datos.Text + "`.`logestado`;";
connection = new MySqlConnection(MyConString);
MySqlCommand con3 = new MySqlCommand(drop_trigger, connection);
connection.Open();
con3.ExecuteNonQuery();
connection.Close();
string trigger = "CREATE TRIGGER `" + textBox_Base_de_datos.Text + "`.`logestado` AFTER UPDATE ON `" + textBox_Base_de_datos.Text + "`.`" + textBox_dispositivo.Text + @"`
FOR EACH ROW BEGIN
DECLARE Rele1 INT;
DECLARE Rele2 INT;
DECLARE Rele3 INT;
DECLARE Rele4 INT;
SET Rele1 = (SELECT estado
FROM dispositivos
WHERE nombre LIKE 'RELE 1');
SET Rele2 = (SELECT estado
FROM dispositivos
WHERE nombre LIKE 'RELE 2');
SET Rele3 = (SELECT estado
FROM dispositivos
WHERE nombre LIKE 'RELE 3');
SET Rele4 = (SELECT estado
FROM dispositivos
WHERE nombre LIKE 'RELE 4');
INSERT INTO " + textBox_log.Text + @"(RELE_1, RELE_2, RELE_3, RELE_4, USUARIO, FECHA_HORA)
VALUES (Rele1, Rele2, Rele3, Rele4, USER(), NOW());
END";
connection = new MySqlConnection(MyConString);
MySqlCommand con4 = new MySqlCommand(trigger, connection);
connection.Open();
con4.ExecuteNonQuery();
connection.Close();
// Volcar la base de datos para la tabla `dispositivos`
string insertar_datos = "INSERT INTO `" + textBox_dispositivo.Text + @"` (`ID`, `nombre`, `estado`, `descripcion`) VALUES
(1, 'RELÉ 1', 0, ' '),
(2, 'RELÉ 2', 0, ' '),
(3, 'RELÉ 3', 0, ' '),
(4, 'RELÉ 4', 0, ' ');";
connection = new MySqlConnection(MyConString);
MySqlCommand con5 = new MySqlCommand(insertar_datos, connection);
connection.Open();
con5.ExecuteNonQuery();
connection.Close();
// Estructura de tabla para la tabla `log`
string log = "CREATE TABLE IF NOT EXISTS `" + textBox_log.Text + @"` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`RELE_1` tinyint(1) NOT NULL,
`RELE_2` tinyint(1) NOT NULL,
`RELE_3` tinyint(1) NOT NULL,
`RELE_4` tinyint(1) NOT NULL,
`USUARIO` varchar(40) COLLATE latin1_spanish_ci NOT NULL,
`FECHA_HORA` datetime NOT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_spanish_ci AUTO_INCREMENT=1;";
connection = new MySqlConnection(MyConString);
MySqlCommand con6 = new MySqlCommand(log, connection);
connection.Open();
con6.ExecuteNonQuery();
connection.Close();
// Deshabilitar.
button_crear_bd.Enabled = false;
textBox_Base_de_datos.Enabled = false;
textBox_dispositivo.Enabled = false;
textBox_log.Enabled = false;
textBox_descripcion_rele1.Enabled = true;
textBox_descripcion_rele2.Enabled = true;
textBox_descripcion_rele3.Enabled = true;
textBox_descripcion_rele4.Enabled = true;
#endregion
}
private void button_aplicar_config_Click(object sender, EventArgs e)
{
groupBox2.Visible = true;
}
private void button_actualizar_Click(object sender, EventArgs e)
{
string insertar_disp = @"UPDATE `dispositivos` SET descripcion='descripcion1' WHERE id='1';
UPDATE `dispositivos` SET descripcion='descripcion2' WHERE id='2';
UPDATE `dispositivos` SET descripcion='descripcion3' WHERE id='3';
UPDATE `dispositivos` SET descripcion='descripcion4' WHERE id='4';";
connection = new MySqlConnection(MyConString);
MySqlCommand con7 = new MySqlCommand(insertar_disp, connection);
connection.Open();
con7.ExecuteNonQuery();
connection.Close();
// Deshabilitar.
button_actualizar.Enabled = false;
}
private void button_borrar_bd_Click(object sender, EventArgs e)
{
string borrar_bd = @"DROP DATABASE pic_remoto;"; // " + textBox_Base_de_datos.Text + "
connection = new MySqlConnection(MyConString);
MySqlCommand con8 = new MySqlCommand(borrar_bd, connection);
connection.Open();
con8.ExecuteNonQuery();
connection.Close();
}
private void button_limpiar_Click(object sender, EventArgs e)
{
textBox_Base_de_datos.Clear();
textBox_dispositivo.Clear();
textBox_log.Clear();
textBox_descripcion_rele1.Clear();
textBox_descripcion_rele2.Clear();
textBox_descripcion_rele3.Clear();
textBox_descripcion_rele4.Clear();
button_predeterminado.Enabled = true;
}
private void button_predeterminado_Click(object sender, EventArgs e)
{
textBox_Base_de_datos.Text = "pic_remoto";
textBox_dispositivo.Text = "dispositivos";
textBox_log.Text = "log";
button_predeterminado.Enabled = false;
}
}
}
__________________ Meta Shell, VERSIÓN 1.2.2 Descargar |