Conexión VB y Mysql
-
Upload
janesyta-cruz -
Category
Documents
-
view
272 -
download
0
description
Transcript of Conexión VB y Mysql
![Page 1: Conexión VB y Mysql](https://reader034.fdocuments.es/reader034/viewer/2022050908/563db857550346aa9a92d389/html5/thumbnails/1.jpg)
Conexión VB .net y Mysql
Ingeniería en Sistemas Computacionales
![Page 2: Conexión VB y Mysql](https://reader034.fdocuments.es/reader034/viewer/2022050908/563db857550346aa9a92d389/html5/thumbnails/2.jpg)
Nuevo proyecto
![Page 3: Conexión VB y Mysql](https://reader034.fdocuments.es/reader034/viewer/2022050908/563db857550346aa9a92d389/html5/thumbnails/3.jpg)
Aplicación de Windows Form
![Page 4: Conexión VB y Mysql](https://reader034.fdocuments.es/reader034/viewer/2022050908/563db857550346aa9a92d389/html5/thumbnails/4.jpg)
Cambiamos el nombre del proyecto
![Page 5: Conexión VB y Mysql](https://reader034.fdocuments.es/reader034/viewer/2022050908/563db857550346aa9a92d389/html5/thumbnails/5.jpg)
Así queda
![Page 6: Conexión VB y Mysql](https://reader034.fdocuments.es/reader034/viewer/2022050908/563db857550346aa9a92d389/html5/thumbnails/6.jpg)
Cambiamos el nombre del formulario por FrmProveedores
![Page 7: Conexión VB y Mysql](https://reader034.fdocuments.es/reader034/viewer/2022050908/563db857550346aa9a92d389/html5/thumbnails/7.jpg)
Guardar Todo…. Aparecerá el nombre del proyecto y damos guardar
![Page 8: Conexión VB y Mysql](https://reader034.fdocuments.es/reader034/viewer/2022050908/563db857550346aa9a92d389/html5/thumbnails/8.jpg)
Agregamos la referencia
![Page 9: Conexión VB y Mysql](https://reader034.fdocuments.es/reader034/viewer/2022050908/563db857550346aa9a92d389/html5/thumbnails/9.jpg)
Veremos la siguiente ventana
![Page 10: Conexión VB y Mysql](https://reader034.fdocuments.es/reader034/viewer/2022050908/563db857550346aa9a92d389/html5/thumbnails/10.jpg)
Buscamos el archivo MySql.Data.dll
![Page 11: Conexión VB y Mysql](https://reader034.fdocuments.es/reader034/viewer/2022050908/563db857550346aa9a92d389/html5/thumbnails/11.jpg)
Si instalamos el conector de mysql entre a esta ubicación
![Page 12: Conexión VB y Mysql](https://reader034.fdocuments.es/reader034/viewer/2022050908/563db857550346aa9a92d389/html5/thumbnails/12.jpg)
Seleccionamos MySql.Data.dll
![Page 13: Conexión VB y Mysql](https://reader034.fdocuments.es/reader034/viewer/2022050908/563db857550346aa9a92d389/html5/thumbnails/13.jpg)
Comprobamos la referencia
![Page 14: Conexión VB y Mysql](https://reader034.fdocuments.es/reader034/viewer/2022050908/563db857550346aa9a92d389/html5/thumbnails/14.jpg)
Entramos a mysql y creamos lo siguiente
![Page 15: Conexión VB y Mysql](https://reader034.fdocuments.es/reader034/viewer/2022050908/563db857550346aa9a92d389/html5/thumbnails/15.jpg)
Comprobamos la conexión agregando el siguiente código
Imports MySql.Data.MySqlClient
Public Class FrmProveedores
Dim conexion As MySqlConnection
Dim cm As MySqlCommand
Private Sub FrmProveedores_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Try
conexion = New MySqlConnection("Data Source=127.0.0.1; Port=3306; Database=sistemita;User ID=root; Password=admin")
conexion.Open()
MessageBox.Show("Conectado a la Base de datos Sistemita", "Mensaje de prueba", MessageBoxButtons.OK)
Catch mierror As MySqlException
MessageBox.Show("Error de Conexión a la Base de Datos: " & mierror.Message)
conexion.Dispose()
End Try
End Sub
End Class
![Page 16: Conexión VB y Mysql](https://reader034.fdocuments.es/reader034/viewer/2022050908/563db857550346aa9a92d389/html5/thumbnails/16.jpg)
Ejecutamos
![Page 17: Conexión VB y Mysql](https://reader034.fdocuments.es/reader034/viewer/2022050908/563db857550346aa9a92d389/html5/thumbnails/17.jpg)
Diseñamos la siguiente GUI
1
2
3
4
5
6
7
8
9
10
1
2
3
4
5
6
7
8
9
10
Nombre de los controles
TxtCodigo
TxtNombre
TxtDireccion
TxtTelefono
TxtRFC
BtnAgregar
BtnActualizar
BtnEliminar
BtnBuscar
BtnVerTodos
![Page 18: Conexión VB y Mysql](https://reader034.fdocuments.es/reader034/viewer/2022050908/563db857550346aa9a92d389/html5/thumbnails/18.jpg)
Botón Agregar
![Page 19: Conexión VB y Mysql](https://reader034.fdocuments.es/reader034/viewer/2022050908/563db857550346aa9a92d389/html5/thumbnails/19.jpg)
Botón actualizar
![Page 20: Conexión VB y Mysql](https://reader034.fdocuments.es/reader034/viewer/2022050908/563db857550346aa9a92d389/html5/thumbnails/20.jpg)
Botón Buscar
![Page 21: Conexión VB y Mysql](https://reader034.fdocuments.es/reader034/viewer/2022050908/563db857550346aa9a92d389/html5/thumbnails/21.jpg)
Boton Eliminar
![Page 22: Conexión VB y Mysql](https://reader034.fdocuments.es/reader034/viewer/2022050908/563db857550346aa9a92d389/html5/thumbnails/22.jpg)
Agregar un nuevo formulario
![Page 23: Conexión VB y Mysql](https://reader034.fdocuments.es/reader034/viewer/2022050908/563db857550346aa9a92d389/html5/thumbnails/23.jpg)
Cambiamos el nombre del formulario
![Page 24: Conexión VB y Mysql](https://reader034.fdocuments.es/reader034/viewer/2022050908/563db857550346aa9a92d389/html5/thumbnails/24.jpg)
Agregamos un datagridview
![Page 25: Conexión VB y Mysql](https://reader034.fdocuments.es/reader034/viewer/2022050908/563db857550346aa9a92d389/html5/thumbnails/25.jpg)
Clic en la esquina superior derecha y clic en agregar origen de datos del proyecto…
![Page 26: Conexión VB y Mysql](https://reader034.fdocuments.es/reader034/viewer/2022050908/563db857550346aa9a92d389/html5/thumbnails/26.jpg)
Seleccionamos Base de datos
![Page 27: Conexión VB y Mysql](https://reader034.fdocuments.es/reader034/viewer/2022050908/563db857550346aa9a92d389/html5/thumbnails/27.jpg)
Clic en conjunto de datos
![Page 28: Conexión VB y Mysql](https://reader034.fdocuments.es/reader034/viewer/2022050908/563db857550346aa9a92d389/html5/thumbnails/28.jpg)
Clic en nueva conexión…
![Page 29: Conexión VB y Mysql](https://reader034.fdocuments.es/reader034/viewer/2022050908/563db857550346aa9a92d389/html5/thumbnails/29.jpg)
Seleccionamos Mysql Database
Sino aparece instale el controlador ODBC
![Page 30: Conexión VB y Mysql](https://reader034.fdocuments.es/reader034/viewer/2022050908/563db857550346aa9a92d389/html5/thumbnails/30.jpg)
Aparece la siguiente ventana y rellenamos los datos
![Page 31: Conexión VB y Mysql](https://reader034.fdocuments.es/reader034/viewer/2022050908/563db857550346aa9a92d389/html5/thumbnails/31.jpg)
Usamos la base de datos sistemita y probar conexión
![Page 32: Conexión VB y Mysql](https://reader034.fdocuments.es/reader034/viewer/2022050908/563db857550346aa9a92d389/html5/thumbnails/32.jpg)
Luego aparecerá esta ventana y seleccionamos SI incluir….
![Page 33: Conexión VB y Mysql](https://reader034.fdocuments.es/reader034/viewer/2022050908/563db857550346aa9a92d389/html5/thumbnails/33.jpg)
Así: y luego clic en Siguiente >
![Page 34: Conexión VB y Mysql](https://reader034.fdocuments.es/reader034/viewer/2022050908/563db857550346aa9a92d389/html5/thumbnails/34.jpg)
Guardamos con un nombre la cadena de conexión. Y clic en Siguiente>
![Page 35: Conexión VB y Mysql](https://reader034.fdocuments.es/reader034/viewer/2022050908/563db857550346aa9a92d389/html5/thumbnails/35.jpg)
Seleccionamos la tabla proveedores y todos los campos hacemos clic en Finalizar
![Page 36: Conexión VB y Mysql](https://reader034.fdocuments.es/reader034/viewer/2022050908/563db857550346aa9a92d389/html5/thumbnails/36.jpg)
Agregamos el siguiente código al botón Ver todos
Donde FrmVerTodos es el nombre del formulario
![Page 37: Conexión VB y Mysql](https://reader034.fdocuments.es/reader034/viewer/2022050908/563db857550346aa9a92d389/html5/thumbnails/37.jpg)
Ejecutamos proveedores y damos clic en botón Ver Todos
![Page 38: Conexión VB y Mysql](https://reader034.fdocuments.es/reader034/viewer/2022050908/563db857550346aa9a92d389/html5/thumbnails/38.jpg)
Ver todos 2 ….. Codificado. Nuevo form
![Page 39: Conexión VB y Mysql](https://reader034.fdocuments.es/reader034/viewer/2022050908/563db857550346aa9a92d389/html5/thumbnails/39.jpg)
Agregamos un datagridview
![Page 40: Conexión VB y Mysql](https://reader034.fdocuments.es/reader034/viewer/2022050908/563db857550346aa9a92d389/html5/thumbnails/40.jpg)
Doble clic en el formulario
![Page 41: Conexión VB y Mysql](https://reader034.fdocuments.es/reader034/viewer/2022050908/563db857550346aa9a92d389/html5/thumbnails/41.jpg)
Creamos una table en mysql
mysql> use sistemitaDatabase changed
mysql> create table productos(id_prod int primary key, descripcion_prod varchar(40), stock_prod int, preciocompra_prod float, precioventa_prod float, existencia_prod int, idprov_prod int, foreign key(idprov_prod) references proveedores(idprov) on delete no action on update cascade) engine=innodb;Query OK, 0 rows affected (1.01 sec)
![Page 42: Conexión VB y Mysql](https://reader034.fdocuments.es/reader034/viewer/2022050908/563db857550346aa9a92d389/html5/thumbnails/42.jpg)
Un nuevo Windows Form FrmProductos
![Page 43: Conexión VB y Mysql](https://reader034.fdocuments.es/reader034/viewer/2022050908/563db857550346aa9a92d389/html5/thumbnails/43.jpg)
Diseñamos la siguiente GUI1
2
3
4
5
6
7
8
9
TxtCodigo
TxtDescripcion
TxtStock
TxtPrecioCompra
TxtPrecioVenta
BtnAgregar
BtnActualizar
BtnEliminar
BtnBuscar
BtnVerTodos
1
2
3
4
5
67
8
9
10
11
12
13
10
11
12
13
TxtExistencia
TxtClaveProveedor
TxtNombreProv
![Page 44: Conexión VB y Mysql](https://reader034.fdocuments.es/reader034/viewer/2022050908/563db857550346aa9a92d389/html5/thumbnails/44.jpg)
Conexión a VB.net con Mysql
Usando un Módulo para creación de Métodos
![Page 45: Conexión VB y Mysql](https://reader034.fdocuments.es/reader034/viewer/2022050908/563db857550346aa9a92d389/html5/thumbnails/45.jpg)
Agregamos un modulo
![Page 46: Conexión VB y Mysql](https://reader034.fdocuments.es/reader034/viewer/2022050908/563db857550346aa9a92d389/html5/thumbnails/46.jpg)
Nómbrelo como: MdlConexion
![Page 47: Conexión VB y Mysql](https://reader034.fdocuments.es/reader034/viewer/2022050908/563db857550346aa9a92d389/html5/thumbnails/47.jpg)
Quedando así:
![Page 48: Conexión VB y Mysql](https://reader034.fdocuments.es/reader034/viewer/2022050908/563db857550346aa9a92d389/html5/thumbnails/48.jpg)
Escribimos la función Conectar
Agregue la librería
Defina los objetos con acceso público
![Page 49: Conexión VB y Mysql](https://reader034.fdocuments.es/reader034/viewer/2022050908/563db857550346aa9a92d389/html5/thumbnails/49.jpg)
En el formulario FrmProductos doble clic en el formulario (excepto los objetos)
![Page 50: Conexión VB y Mysql](https://reader034.fdocuments.es/reader034/viewer/2022050908/563db857550346aa9a92d389/html5/thumbnails/50.jpg)
Programando el botón Agregar
Uso del Try Catchpara interpretar los erroresenviados desde MYSQL
![Page 51: Conexión VB y Mysql](https://reader034.fdocuments.es/reader034/viewer/2022050908/563db857550346aa9a92d389/html5/thumbnails/51.jpg)
Botón eliminar
![Page 52: Conexión VB y Mysql](https://reader034.fdocuments.es/reader034/viewer/2022050908/563db857550346aa9a92d389/html5/thumbnails/52.jpg)
Botón Actualizar
![Page 53: Conexión VB y Mysql](https://reader034.fdocuments.es/reader034/viewer/2022050908/563db857550346aa9a92d389/html5/thumbnails/53.jpg)
Ejemplo de buscar desde el modulo + consulta multitabla
![Page 54: Conexión VB y Mysql](https://reader034.fdocuments.es/reader034/viewer/2022050908/563db857550346aa9a92d389/html5/thumbnails/54.jpg)
En el botón Buscar de FrmProductos escriba:
![Page 55: Conexión VB y Mysql](https://reader034.fdocuments.es/reader034/viewer/2022050908/563db857550346aa9a92d389/html5/thumbnails/55.jpg)
Desde el modulo programas limpiar las cajas de texto (SOLO TEXTBOX)
![Page 56: Conexión VB y Mysql](https://reader034.fdocuments.es/reader034/viewer/2022050908/563db857550346aa9a92d389/html5/thumbnails/56.jpg)
Uso de la función limpiar en los formularios
![Page 57: Conexión VB y Mysql](https://reader034.fdocuments.es/reader034/viewer/2022050908/563db857550346aa9a92d389/html5/thumbnails/57.jpg)
Seleccionar el cuadro de texto TxtCodigo
![Page 58: Conexión VB y Mysql](https://reader034.fdocuments.es/reader034/viewer/2022050908/563db857550346aa9a92d389/html5/thumbnails/58.jpg)
Seleccionamos el evento KeyPress
![Page 59: Conexión VB y Mysql](https://reader034.fdocuments.es/reader034/viewer/2022050908/563db857550346aa9a92d389/html5/thumbnails/59.jpg)
Se agregará en automático el siguiente codigo
![Page 60: Conexión VB y Mysql](https://reader034.fdocuments.es/reader034/viewer/2022050908/563db857550346aa9a92d389/html5/thumbnails/60.jpg)
Capturamos el siguiente codigo
If Char.IsDigit(e.KeyChar) Then e.Handled = False ElseIf Char.IsControl(e.KeyChar) Then e.Handled = False Else e.Handled = True End If
![Page 61: Conexión VB y Mysql](https://reader034.fdocuments.es/reader034/viewer/2022050908/563db857550346aa9a92d389/html5/thumbnails/61.jpg)
Quedando finalmente así:
![Page 62: Conexión VB y Mysql](https://reader034.fdocuments.es/reader034/viewer/2022050908/563db857550346aa9a92d389/html5/thumbnails/62.jpg)
Enter
![Page 63: Conexión VB y Mysql](https://reader034.fdocuments.es/reader034/viewer/2022050908/563db857550346aa9a92d389/html5/thumbnails/63.jpg)
Escribir:
![Page 64: Conexión VB y Mysql](https://reader034.fdocuments.es/reader034/viewer/2022050908/563db857550346aa9a92d389/html5/thumbnails/64.jpg)
![Page 65: Conexión VB y Mysql](https://reader034.fdocuments.es/reader034/viewer/2022050908/563db857550346aa9a92d389/html5/thumbnails/65.jpg)
Segunda opción
![Page 66: Conexión VB y Mysql](https://reader034.fdocuments.es/reader034/viewer/2022050908/563db857550346aa9a92d389/html5/thumbnails/66.jpg)
Usando buscar al realizar el enter
![Page 67: Conexión VB y Mysql](https://reader034.fdocuments.es/reader034/viewer/2022050908/563db857550346aa9a92d389/html5/thumbnails/67.jpg)
Agregar un nuevo Windows Form nómbrelo FrmLogin
![Page 68: Conexión VB y Mysql](https://reader034.fdocuments.es/reader034/viewer/2022050908/563db857550346aa9a92d389/html5/thumbnails/68.jpg)
Nuevo form tipo MDI
![Page 69: Conexión VB y Mysql](https://reader034.fdocuments.es/reader034/viewer/2022050908/563db857550346aa9a92d389/html5/thumbnails/69.jpg)
![Page 70: Conexión VB y Mysql](https://reader034.fdocuments.es/reader034/viewer/2022050908/563db857550346aa9a92d389/html5/thumbnails/70.jpg)
![Page 71: Conexión VB y Mysql](https://reader034.fdocuments.es/reader034/viewer/2022050908/563db857550346aa9a92d389/html5/thumbnails/71.jpg)
![Page 72: Conexión VB y Mysql](https://reader034.fdocuments.es/reader034/viewer/2022050908/563db857550346aa9a92d389/html5/thumbnails/72.jpg)
![Page 73: Conexión VB y Mysql](https://reader034.fdocuments.es/reader034/viewer/2022050908/563db857550346aa9a92d389/html5/thumbnails/73.jpg)