// Compilación:
// mcs -r:System.Drawing.dll -r:System.Windows.Forms.dll -r:/usr/lib/mono/4.5/Microsoft.VisualBasic.dll -r:System.Data.dll -r:/home/ariel/Documentos/librerias/mysql-connector-net-6.9.6-noinstall/v4.0/MySql.Data.dll ConexionMySQL.cs
//Ejecución:
// mono ConexionMySQL.exe
//Autor: Fer Carraro
using System.Windows.Forms;
using Microsoft.VisualBasic;
using System;
using System.Data;
using MySql.Data.MySqlClient;
public class ConexionMySQL{
public static void Main(string[] args){
string id=null;
string nombre=null;
string apellido_p=null;
string apellido_m=null;
int edad=0;
double peso=0.0;
double talla=0.0;
ConexionMySQL conexion=null;
try{
conexion=new ConexionMySQL();
conexion.conectar();
while(true){
int m=menu();
switch(m){
case 1: Console.WriteLine("Insertar");
nombre=Interaction.InputBox("Introduce nombre: ","Insercion a la BD","",100,100).ToString();
apellido_p=Interaction.InputBox("Introduce apellido paterno: ","Insercion a la BD","",100,100).ToString();
apellido_m=Interaction.InputBox("Introduce apellido materno: ","Insercion a la BD","",100,100).ToString();
edad=int.Parse(Interaction.InputBox("Introduce edad: ","Insercion a la BD","",100,100).ToString());
peso=double.Parse(Interaction.InputBox("Introduce peso (kg): ","Insercion a la BD","",100,100).ToString());
talla=double.Parse(Interaction.InputBox("Introduce talla (mt): ","Insercion a la BD","",100,100).ToString());
conexion.insertar(nombre,apellido_p,apellido_m,edad,peso,talla);
break;
case 2: Console.WriteLine("Eliminar");
id=Interaction.InputBox("Introduce clave (id) de la persona a eliminar: ","Consulta a la BD","",100,100).ToString();
conexion.eliminar(id);
break;
case 3: Console.WriteLine("Modificar");
id=Interaction.InputBox("Introduce clave (id) de la persona a actualizar: ","Consulta a la BD","",100,100).ToString();
break;
case 4: Console.WriteLine("Consultar");
id=Interaction.InputBox("Introduce clave (id) a buscar: ","Consulta a la BD","",100,100).ToString();
conexion.consultar(id);
break;
case 5: conexion.desconectar(); Environment.Exit(0);
break;
default: MessageBox.Show(null,"No existe esa opcion","Error",MessageBoxButtons.OK,MessageBoxIcon.Error);break;
}//fin switch
}//fin while
}catch(Exception ex){
Console.Error.WriteLine("Error {0}",ex.Message);
Environment.Exit(0);
}
}
public static int menu(){
int opc=0;
string opciones="\tOpciones(5 para salir) \n1. Insertar datos\n2. Eliminar datos"
+"\n3. Modificar datos\n4. Consultar datos";
opc=int.Parse(Interaction.InputBox(opciones,"Menu del programa","",100,200).ToString());
return opc;
}
}
class ConexionMySQL{
public const string CONNECT= "Server=localhost;" +
"Database=test;" +
"User ID=root;" +
"Password=5432;" +
"Pooling=false";
IDbConnection dbcon;
IDbCommand dbcmd;
//para insertar
//https://social.msdn.microsoft.com/Forums/vstudio/en-US/67226fe6-175a-4f8b-96ab-a22fd836617f/inserting-values-to-mysql-using-c?forum=csharpgeneral
MySqlCommand cmd = new MySql.Data.MySqlClient.MySqlCommand();
public ConexionMySQL(){Console.WriteLine("objeto ConexionMySQL creado");}
public void conectar(){
dbcon = new MySqlConnection(CONNECT);
dbcon.Open();
Console.WriteLine("conexion establecida");
dbcmd= dbcon.CreateCommand();
}
public void desconectar(){
dbcmd.Dispose();
dbcmd = null;
dbcon.Close();
dbcon = null;
Console.WriteLine("conexion terminada");
}
public void consultar(string id){
string sql =
"SELECT nombre, apellidoP " +
"FROM persona where id="+id;
dbcmd.CommandText = sql;
IDataReader reader = dbcmd.ExecuteReader();
while(reader.Read()) {
string FirstName = (string) reader["nombre"];
string LastName = (string) reader["apellidoP"];
MessageBox.Show("Nombre: " +FirstName + " " + LastName,"Datos de la consulta",MessageBoxButtons.OK);
}
reader.Close();
reader = null;
Console.WriteLine("Consulta terminada");
}
public void insertar(string nombre,string apellidoP,string apellidoM,int edad,double peso,double talla){
string sql = "insert into test.persona(nombre, apellidoP, apellidoM, edad, peso, talla) values('"
+nombre+ "','" +apellidoP+ "','"
+apellidoM+ "','" +edad+ "','" +peso+"','"
+talla+ "');";
dbcmd.CommandText = sql;
dbcmd.ExecuteNonQuery();
Console.WriteLine("Insercion terminada");
}
public void eliminar(string id){
string sql = "delete from test.persona where id='" + id + "';";
dbcmd.CommandText = sql;
dbcmd.ExecuteNonQuery();
Console.WriteLine("Eliminacion terminada");
}
public void actualizar(string id,string nombre,string apellidoP,string apellidoM,int edad,double peso,double talla){
string sql = "update test.persona set nombre='"
+ nombre + "',apellidoP='" + apellidoP
+ "',apellidoM='" + apellidoM + "',edad='" + edad + "',peso='" +
+ peso + "',talla='" + talla + "' where id='" + id + "';";
dbcmd.CommandText = sql;
dbcmd.ExecuteNonQuery();
Console.WriteLine("Actualizacion terminada");
}
}
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.