-
Notifications
You must be signed in to change notification settings - Fork 0
/
registro.cs
85 lines (75 loc) · 2.54 KB
/
registro.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
using MySql.Data.MySqlClient;
using System;
namespace practica8
{
public class registro : conexion
{
public registro()
{
}
public void mostrarconexion(){
this.abrirConexion();
MySqlCommand myCommand = new MySqlCommand(this.querySelect(),
myConnection);
MySqlDataReader myReader = myCommand.ExecuteReader();
while (myReader.Read()){
string id = myReader["id"].ToString();
string nombre = myReader["Nombre"].ToString();
string codigo = myReader["Codigo"].ToString();
string telefono = myReader["Telefono"].ToString();
string email = myReader["Email"].ToString();
Console.WriteLine("ID: " + id +
" Nombre: " + nombre +
" Codigo: " + codigo +
" Telefono:" + telefono +
" Email:" + email);
}
myReader.Close();
myReader = null;
myCommand.Dispose();
myCommand = null;
this.cerrarConexion();
}
public void agregarregistro(string nombre, string codigo, string telefono, string email){
this.abrirConexion();
string sql="INSERT INTO `datos`(`nombre`, `codigo`, `telefono`, `email`) VALUES ('"+nombre+"','"+codigo+"','"+telefono+"','"+email+"')";
this.ejecutarComando(sql);
this.cerrarConexion();
}
public void editarRegistro(string id, string nombre,string codigo, string telefono, string email){
Console.WriteLine("Seguro que desea Editarlo");
Console.WriteLine ("n=No, s=SI");
string opc = Console.ReadLine();
if (opc!="n"){
this.abrirConexion();
string sql = "UPDATE `datos` SET `nombre`='" + nombre + "',`codigo`='" + codigo + "',`telefono`='" + telefono + "',`email`='" + email + "' WHERE (`id`='" + id + "')";
this.ejecutarComando(sql);
this.cerrarConexion();
Console.WriteLine("Se edito con exito");
}
}
public void eliminarRegistroPorId(string id){
Console.WriteLine("desea eliminarlo");
Console.WriteLine ("n=No, s=SI");
string opc = Console.ReadLine();
if (opc!="n"){
this.abrirConexion();
string sql = "DELETE FROM `datos` WHERE (`id`='" + id + "')";
this.ejecutarComando(sql);
this.cerrarConexion();
Console.WriteLine("Se elimino con exito");
}
}
private int ejecutarComando(string sql){
MySqlCommand myCommand = new MySqlCommand(sql,this.myConnection);
int afectadas = myCommand.ExecuteNonQuery();
myCommand.Dispose();
myCommand = null;
return afectadas;
}
private string querySelect(){
return "SELECT * " +
"FROM datos";
}
}
}