Académique Documents
Professionnel Documents
Culture Documents
PRESENTA:
Elioena Garca Garca
MATRICULA:
13ISC215
ASESOR:
Ing. Eloy Snchez Salmorn
MATERIA:
Tpicos avanzados de programacin
MODULO:
Unidad 3: componentes y libreras
ACTIVIDAD:
Reporte de Prcticas
FECHA:
12 de Mayo del 2015
TTULO:
Manejo de componentes y libreras
Contenido
INTRODUCCIN................................................................................................. 3
3.1 PROGRAMA QUE COMPRUEBA LA CONEXIN CON MYSQL..........................4
3.2 PRINTSCREEN DE EJECUCIN DEL PROGRAMA...........................................7
3.3 CRUP CON MYSQL....................................................................................... 8
INTRODUCCIN
Clase 1:
using
using
using
using
using
using
using
using
System;
System.Collections.Generic;
System.Linq;
System.Text;
System.Threading.Tasks;
MySql.Data.MySqlClient;
MySql.Data.Types;
System.Windows.Forms;
namespace Unidad3
{
class MYSQL
{
public MySqlConnection conexion;
public bool exito;
public MYSQL() {
conexion = Conexion.connection();
try
{
conexion.Open();
exito = true;
}
catch {
exito = false;
}
}
}
}
Clase 2:
using
using
using
using
using
using
using
System;
System.Collections.Generic;
System.Linq;
System.Text;
System.Threading.Tasks;
MySql.Data.MySqlClient;
MySql.Data.Types;
using System.Windows.Forms;
namespace Unidad3
{
class MYSQL
{
public MySqlConnection conexion;
public bool exito;
public MYSQL() {
conexion = Conexion.connection();
try
{
conexion.Open();
exito = true;
}
catch {
exito = false;
}
}
Clase 3:
using
using
using
using
using
using
using
System;
System.Collections.Generic;
System.Linq;
System.Text;
System.Threading.Tasks;
MySql.Data.MySqlClient;
MySql.Data.Types;
namespace Unidad3
{
//creamos una clase abstracta
public abstract class Conexion
{
// metodo connection que devuelve un valor de tipo mysqlConection
public static MySqlConnection connection() {
MySqlConnection con = new MySqlConnection("SERVER =localhost;
DATABASE=pruebas;UID=root");
return con;
}
}
}
Clase 4:
using
using
using
using
using
System;
System.Collections.Generic;
System.Linq;
System.Threading.Tasks;
System.Windows.Forms;
namespace Unidad3
{
static class Program
{
/// <summary>
/// Punto de entrada principal para la aplicacin.
/// </summary>
// [STAThread]
public static MYSQL con = new MYSQL();
static void Main()
{
if (con.exito == true)
{
MessageBox.Show("Conexion existosa", "MYSQL",
MessageBoxButtons.OK, MessageBoxIcon.Information);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
else {
MessageBox.Show("Error al conectar la base de
datos","Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
Clase 1:
using
using
using
using
using
using
using
System;
System.Collections.Generic;
System.Linq;
System.Text;
System.Threading.Tasks;
MySql.Data.MySqlClient;
MySql.Data.Types;
namespace CONBASESDED
{
public abstract class BdComun
{
public static MySqlConnection ObtenerConexion()
{
string cadena = "SERVER=localhost;DATABASE=pruebas;UID=root;";
MySqlConnection conexion = new MySqlConnection(cadena);
try
{
conexion.Open();
// return conexion;
}
catch {
throw;
}
}
}
return conexion;
Clase 2:
using
using
using
using
using
System;
System.Collections.Generic;
System.Linq;
System.Text;
System.Threading.Tasks;
namespace CONBASESDED
{
public class Cliente
{
public
public
public
public
public
Clase 3:
sing System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;
namespace CONBASESDED
{
class ClienteDAL
{
public static int Agregar(Cliente pUsuario) {
int retorno= 0;
//string sql="";
MySqlCommand comando = new MySqlCommand(
string.Format(
"INSERT INTO cliente (nombre, apellido, fechaNac,direccion)VALUES
('{0}','{1}','{2}','{3}')",
pUsuario.Nombre,pUsuario.Apellidos,pUsuario.FechaNac,pUsuario.Direccion),BdCo
mun.ObtenerConexion());
retorno=comando.ExecuteNonQuery();
return retorno;
}
public static List<Cliente> Buscar(string pNombre, string pApellidos) {
List<Cliente> lista = new List<Cliente>();
MySqlCommand comando=new MySqlCommand(
10
string.Format("SELECT codigo,nombre,apellido,fechaNac,direccion
FROM cliente WHERE nombre='{0}' OR apellido='{1}'",
pNombre,pApellidos),BdComun.ObtenerConexion());
MySqlDataReader reader = comando.ExecuteReader();
while (reader.Read()) {
Cliente pUsuario = new Cliente();
pUsuario.Codigo = reader.GetInt32(0);
pUsuario.Nombre = reader.GetString(1);
pUsuario.Apellidos = reader.GetString(2);
pUsuario.FechaNac = reader.GetString(3);
pUsuario.Direccion = reader.GetString(4);
lista.Add(pUsuario);
}
return lista;
}
public static Cliente ObtenerCliente(int pCodigo) {
Cliente pUsuario = new Cliente();
MySqlConnection conexion = BdComun.ObtenerConexion();
MySqlCommand comando = new MySqlCommand(String.Format("SELECT
codigo,nombre,apellido,fechaNac,direccion FROM cliente WHERE codigo={0}",
pCodigo), conexion);
MySqlDataReader reader = comando.ExecuteReader();
while (reader.Read())
{
pUsuario.Codigo = reader.GetInt32(0);
pUsuario.Nombre = reader.GetString(1);
pUsuario.Apellidos = reader.GetString(2);
pUsuario.FechaNac = reader.GetString(3);
pUsuario.Direccion = reader.GetString(4);
}
conexion.Close();
return pUsuario;
11
conexion.Close();
return retorno;
}
public static int Eliminar (double pCoodigo){
int retorno=0;
MySqlConnection conexion=BdComun.ObtenerConexion();
MySqlCommand comando=new MySqlCommand(string.Format("DELETE
FROM cliente WHERE codigo ={0}",pCoodigo),conexion);
retorno =comando.ExecuteNonQuery();
conexion.Close();
return retorno;
}
}
Clase 4:
using
using
using
using
using
using
using
using
using
System;
System.Collections.Generic;
System.ComponentModel;
System.Data;
System.Drawing;
System.Linq;
System.Text;
System.Threading.Tasks;
System.Windows.Forms;
namespace CONBASESDED
{
public partial class frmenai : Form
{
public Cliente ClienteSeleccionado { get; set; }
public frmenai()
{
InitializeComponent();
}
private void btnBuscar_Click(object sender, EventArgs e)
{
dataGridView1.DataSource = ClienteDAL.Buscar(txtNomE.Text,
txtApelliE.Text);
}
private void btnAceptar_Click(object sender, EventArgs e)
{
if (dataGridView1.SelectedRows.Count == 1)
12
int id = Convert.ToInt32(dataGridView1.CurrentRow.Cells[0].Value);
ClienteSeleccionado = ClienteDAL.ObtenerCliente(id);
this.Close();
}
else {
MessageBox.Show("Debe seleccionar una fila");
}
}
Clase 5:
using
using
using
using
using
using
using
using
using
using
using
System;
System.Collections.Generic;
System.ComponentModel;
System.Data;
System.Drawing;
System.Linq;
System.Text;
System.Threading.Tasks;
System.Windows.Forms;
MySql.Data.MySqlClient;
MySql.Data.Types;
namespace CONBASESDED
{
public partial class frmPrincipal : Form
{
public Cliente clienteActual { get; set; }
public frmPrincipal()
{
InitializeComponent();
}
private void btnGuardar_Click(object sender, EventArgs e)
{
if (string.IsNullOrWhiteSpace(txtNombre.Text) ||
string.IsNullOrWhiteSpace(txtApellidos.Text) ||
string.IsNullOrWhiteSpace(txtDireccion.Text))
MessageBox.Show("Hay uno om mas campos vacios", "campos vacios",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
else
{
13
14
}
private void btnEliminar_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Esta Seguro que desea eliminar el Cliente Actual",
"Estas Seguro??", MessageBoxButtons.YesNo, MessageBoxIcon.Question) ==
DialogResult.Yes)
15
if (ClienteDAL.Eliminar(clienteActual.Codigo) > 0)
{
MessageBox.Show("Cliente Eliminado Correctamente!", "Cliente
Eliminado", MessageBoxButtons.OK, MessageBoxIcon.Information);
limpiar();
deshabilitar();
}
else
{
MessageBox.Show("No se pudo eliminar el Cliente", "Cliente No
Eliminado", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
else
MessageBox.Show("Se cancelo la eliminacion", "Eliminacion
Cancelada", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
private void btnBuscar_Click(object sender, EventArgs e)
{
frmenai buscar = new frmenai();
buscar.ShowDialog();
if (buscar.ClienteSeleccionado != null)
{
clienteActual = buscar.ClienteSeleccionado;
txtNombre.Text = buscar.ClienteSeleccionado.Nombre;
txtApellidos.Text = buscar.ClienteSeleccionado.Apellidos;
txtDireccion.Text = buscar.ClienteSeleccionado.Direccion;
dateTimePicker1.Text = buscar.ClienteSeleccionado.FechaNac;
btnActualizar.Enabled = true;
btnEliminar.Enabled = true;
Habilitar();
btnGuardar.Enabled = false;
}
private void btnCancelar_Click(object sender, EventArgs e)
{
limpiar();
deshabilitar();
}
private void frmPrincipal_Load(object sender, EventArgs e)
{
deshabilitar();
}
}
16
Clase 6:
sing System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CONBASESDED
{
class ClassProducto
{
public double CodigoBarra { get; set; }
public string NombreProducto { get; set; }
public string FechaCad { get; set; }
public ClassProducto() { }
public ClassProducto(double pCodigob,string pNombreP,string pFechaCad) {
this.CodigoBarra = pCodigob;
this.NombreProducto = pNombreP;
this.FechaCad = pFechaCad;
}
Clase 7:
using
using
using
using
using
using
using
System;
System.Collections.Generic;
System.Linq;
System.Text;
System.Threading.Tasks;
MySql.Data.MySqlClient;
MySql.Data.Types;
namespace CONBASESDED
{
class ProductoDAL
{
public static double Agregar(ClassProducto Productoss)
{
double retorno = 0.00;
MySqlCommand comando = new MySqlCommand(
17
String.Format(
"Insert into productos (codigo,producto,fechaCad)
values('{0}','{1}','{2}')",
Productoss.CodigoBarra,Productoss.NombreProducto,Productoss.FechaCad),BdCo
mun.ObtenerConexion());
retorno =comando.ExecuteNonQuery();
return retorno;
}
public static List<ClassProducto> Buscar(double CodigoBarra)
{
List<ClassProducto> lista = new List<ClassProducto>();
MySqlCommand comando = new MySqlCommand(string.Format(
"SELECT codigo,producto,fechaCad FROM productos where codigo
='{0}'", CodigoBarra), BdComun.ObtenerConexion());
MySqlDataReader leer = comando.ExecuteReader();
while (leer.Read()) {
ClassProducto Producto = new ClassProducto();
Producto.CodigoBarra = leer.GetDouble(0);
Producto.NombreProducto = leer.GetString(1);
Producto.FechaCad = leer.GetString(2);
lista.Add(Producto);
}
return lista;
}
public static ClassProducto ObtenerProducto(double CodBarra) {
ClassProducto Producto = new ClassProducto();
MySqlConnection conexion = BdComun.ObtenerConexion();
MySqlCommand comando = new MySqlCommand(String.Format("SELECT
codigo,producto,fechaCad FROM productos where codigo={0}", CodBarra),
conexion);
MySqlDataReader reader = comando.ExecuteReader();
while(reader.Read()){
Producto.CodigoBarra = reader.GetDouble(0);
Producto.NombreProducto = reader.GetString(1);
Producto.FechaCad = reader.GetString(2);
}
conexion.Close();
return Producto;
18
return retorno;
}
}
Clase 8:
using
using
using
using
using
using
using
using
using
System;
System.Collections.Generic;
System.ComponentModel;
System.Data;
System.Drawing;
System.Linq;
System.Text;
System.Threading.Tasks;
System.Windows.Forms;
namespace CONBASESDED
{
19
dtvRegistro.DataSource = ProductoDAL.Buscar(i);
}
private void btnAceptar_Click(object sender, EventArgs e)
{
if (dtvRegistro.SelectedRows.Count == 1)
{
double id = Convert.ToDouble(dtvRegistro.CurrentRow.Cells[0].Value);
ProductoSeleccionado = ProductoDAL.ObtenerProducto(id);
this.Close();
}
else
MessageBox.Show("Debe seleccionar una fila");
}
private void btnCancelar_Click(object sender, EventArgs e)
{
this.Close();
}
Clase 9:
using
using
using
using
using
using
using
using
using
using
using
System;
System.Collections.Generic;
System.ComponentModel;
System.Data;
System.Drawing;
System.Linq;
System.Text;
System.Threading.Tasks;
System.Windows.Forms;
MySql.Data.MySqlClient;
MySql.Data.Types;
namespace CONBASESDED
{
public partial class FrmProducto : Form
{
internal ClassProducto ProductoActual { get; set; }
public FrmProducto()
{
InitializeComponent();
}
20
void limpiar()
{
tbxCodigob.Clear();
tbxProducto.Clear();
dtpFecha.ResetText();
void Habilitar()
{
tbxCodigob.Enabled = true;
tbxProducto.Enabled = true;
dtpFecha.Enabled = true;
btnGuardar.Enabled = true;
btnCancelar.Enabled = true;
}
void deshabilitar()
{
tbxCodigob.Enabled = false;
tbxProducto.Enabled = false;
dtpFecha.Enabled = false;
btnGuardar.Enabled = false;
btnEliminar.Enabled = false;
btnActualizar.Enabled = false;
btnCancelar.Enabled = false;
btnNuevo.Enabled = true;
21
22
if (ProductoDAL.Actualizar(Producto) > 0)
{
MessageBox.Show("Los datos del producto se actualizaron", "Datos
Actualizados", MessageBoxButtons.OK, MessageBoxIcon.Information);
limpiar();
deshabilitar();
}
else
{
MessageBox.Show("No se pudo actualizar", "Error al Actualizar",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
23
buscar.ShowDialog();
if (buscar.ProductoSeleccionado != null)
{
ProductoActual = buscar.ProductoSeleccionado;
tbxCodigob.Text =
Convert.ToString(buscar.ProductoSeleccionado.CodigoBarra);
tbxProducto.Text = buscar.ProductoSeleccionado.NombreProducto;
dtpFecha.Text = buscar.ProductoSeleccionado.FechaCad;
btnActualizar.Enabled = true;
btnEliminar.Enabled = true;
Habilitar();
btnGuardar.Enabled = false;
}
Clase 10:
using
using
using
using
using
using
using
using
using
System;
System.Collections.Generic;
System.ComponentModel;
System.Data;
System.Drawing;
System.Linq;
System.Text;
System.Threading.Tasks;
System.Windows.Forms;
24
namespace CONBASESDED
{
public partial class FrmPaginaPrincipal : Form
{
public FrmPaginaPrincipal()
{
InitializeComponent();
}
private void salirToolStripMenuItem_Click(object sender, EventArgs e)
{
DialogResult opcion;
opcion = MessageBox.Show("Archivo", "Desea salir....!!!?",
MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning);
if (opcion == DialogResult.Yes)
{
Close();
}
else if (opcion == DialogResult.No)
{
MessageBox.Show("Has pulsado -No-");
}
else
{
MessageBox.Show("Has pulsado Cancel");
}
}
private void clientesToolStripMenuItem_Click(object sender, EventArgs e)
{
frmPrincipal enai = new frmPrincipal();
enai.ShowDialog();
}
private void salirToolStripMenuItem_MouseEnter(object sender, EventArgs e)
{
ToolStripMenuItem obj = (ToolStripMenuItem)sender;
if (obj == salirToolStripMenuItem)
{
tspMensaje.Text = "salir Ventana... ";
}
}
private void salirToolStripMenuItem_MouseLeave(object sender, EventArgs e)
{
tspMensaje.Text = "Listo";
25
}
private void clientesToolStripMenuItem_MouseEnter(object sender,
EventArgs e)
{
ToolStripMenuItem obj = (ToolStripMenuItem)sender;
if (obj == tspClientes)
{
tspMensaje.Text = "ventana Cliente... ";
}
}
private void tspClientes_MouseLeave(object sender, EventArgs e)
{
tspMensaje.Text = "Listo";
}
private void productosToolStripMenuItem_MouseEnter(object sender,
EventArgs e)
{
ToolStripMenuItem obj = (ToolStripMenuItem)sender;
if (obj == tspProductos)
{
tspMensaje.Text = "ventana Producto... ";
}
}
private void tspProductos_MouseLeave(object sender, EventArgs e)
{
tspMensaje.Text = "Listo";
}
private void tspProductos_Click(object sender, EventArgs e)
{
FrmProducto productos = new FrmProducto();
productos.ShowDialog();
}
private void generarReportesToolStripMenuItem_Click(object sender,
EventArgs e)
{
FrmReportes reportes = new FrmReportes();
reportes.ShowDialog();
reportes.MdiParent = this;
}
}
26
27
28
CONCLUSIN.
La prctica de esta unidad que fue elaborar una aplicacin que se conectara con
mysql y que realizara las consultas bsicas, despus de conocer esta nueva
herramienta que son las operaciones de consultas.
El tema de los componentes y libreras es muy relevante porque nos permite
visualizar cosas que estn atrs de objetos, el funcionamiento de manera interna
de cada componente
29
BIBLIOGRAFIA
John Sharp. (2012). Microsoft Visual C# 2012.Mc Graw Hill. Mexico.pags. 1027
30