IESTP MANUEL SEOANE CORRALES CONEXIÓN A BASE DE DATOS – 2 1. EJECUTAREMOS EL SIGUEINTE SCRIPT EN SQL-SERVER : MANTENIMIENTO PROGRAMACIÓN DISTRIBUIDA CREATE DATABASE MANTENIMIENTO GO USE MANTENIMIENTO GO CREATE TABLE ARTICULOS (ID_ARTICULO INT PRIMARY KEY, NOMBRE_ARTICULO VARCHAR(30), MARCA VARCHAR(30), PRECIO MONEY, STOCK INT) GO INSERT INTO ARTICULOS VALUES ('101','MONITOR','LG','400','1000') INSERT INTO ARTICULOS VALUES ('102','MOUSE','GENIUS','20','1000') 2. EN VISUAL BASIC.NET DISEÑAREMOS EL SIGUIENTE FORMULARIO, EL CUAL DEBE CONECTARSE A LA BASE DE DATOS CREADA 3. CODIFICAR EL FORMULARIO Dim cnn As New SqlConnection("Data Source=OMARCITOS;Initial Catalog=MANTENIMIENTO;Integrated Security=True") ‘Debes generar tu conexión con el servidor de tu Pc Imports System.Data.SqlClient Public Class Form1 Sub CARGAR() Dim cnn As New SqlConnection("…………") Dim cadena As String cadena = "Select * from articulos" Dim cmd As New SqlCommand(cadena, cnn) cmd.CommandType = CommandType.Text Dim da As New SqlDataAdapter(cmd) Dim dt As New DataTable da.Fill(dt) DataGridView1.DataSource = dt End Sub Lic. Omar Angel Mamani Rodriguez docentes.msc@gmail.com IESTP MANUEL SEOANE CORRALES PROGRAMACIÓN DISTRIBUIDA Sub LIMPIAR() TextBox1.Text = "" TextBox2.Text = "" TextBox3.Text = "" TextBox4.Text = "" TextBox5.Text = "" TextBox1.Focus() End Sub Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load CARGAR() End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Try Dim cnn As New SqlConnection("Data Source=OMARCITOS;Initial Catalog=MANTENIMIENTO;Integrated Security=True") Dim cadena As String cnn.Open() cadena = "INSERT INTO ARTICULOS VALUES ('" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "','" & TextBox4.Text & "','" & TextBox5.Text & "')" Dim cmd As New SqlCommand(cadena, cnn) cmd.CommandType = CommandType.Text cmd.ExecuteNonQuery() CARGAR() MsgBox("Los datos se guardaron correctamente") LIMPIAR() Catch ex As Exception MessageBox.Show(ex.Message) End Try End Sub Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click Try Dim cnn As New SqlConnection("Data Source=OMARCITOS;Initial Catalog=MANTENIMIENTO;Integrated Security=True") Dim cadena As String cnn.Open() cadena = "UPDATE ARTICULOS SET ID_ARTICULO='" & TextBox1.Text & "', NOMBRE_ARTICULO='" & TextBox2.Text & "',MARCA='" & TextBox3.Text & "',PRECIO='" & TextBox4.Text & "',STOCK='" & TextBox5.Text & "' WHERE ID_ARTICULO='" & TextBox1.Text & "'" Dim cmd As New SqlCommand(cadena, cnn) cmd.CommandType = CommandType.Text cmd.ExecuteNonQuery() CARGAR() MsgBox("Los datos se guardaron correctamente") LIMPIAR() Catch ex As Exception MessageBox.Show(ex.Message) End Try End Sub Lic. Omar Angel Mamani Rodriguez docentes.msc@gmail.com IESTP MANUEL SEOANE CORRALES PROGRAMACIÓN DISTRIBUIDA Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click Try If TextBox1.Text <> "" Then Dim R R = MsgBox("Desea eliminar", vbYesNo + vbQuestion, "Pregunta") If R = vbYes Then Dim cnn As New SqlConnection("Data Source=OMARCITOS;Initial Catalog=MANTENIMIENTO;Integrated Security=True") Dim cadena As String cnn.Open() cadena = "DELETE FROM ARTICULOS WHERE ID_ARTICULO='" & TextBox1.Text & "'" Dim cmd As New SqlCommand(cadena, cnn) cmd.CommandType = CommandType.Text cmd.ExecuteNonQuery() CARGAR() LIMPIAR() MsgBox("Los datos se ELIMINARON") Else MsgBox("NO SE ELIMINO NADA") End If Else MsgBox("INGRESE UN VALOR EN EL ID_ARTICULO-NO SE ELIMINO NADA") End If Catch ex As Exception MessageBox.Show(ex.Message) End Try End Sub Private Sub DataGridView1_RowHeaderMouseClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles DataGridView1.RowHeaderMouseClick TextBox1.Text = DataGridView1.SelectedRows(0).Cells(0).Value TextBox2.Text = DataGridView1.SelectedRows(0).Cells(1).Value TextBox3.Text = DataGridView1.SelectedRows(0).Cells(2).Value TextBox4.Text = DataGridView1.SelectedRows(0).Cells(3).Value TextBox5.Text = DataGridView1.SelectedRows(0).Cells(4).Value End Sub Private Sub TextBox6_TextChanged(sender As Object, e As EventArgs) Handles TextBox6.TextChanged Dim cnn As New SqlConnection("Data Source=OMARCITOS;Initial Catalog=MANTENIMIENTO;Integrated Security=True") Dim cadena As String If ComboBox1.Text = "NOMBRE_ARTICULO" Then cadena = "SELECT * FROM ARTICULOS WHERE NOMBRE_ARTICULO LIKE '" & TextBox6.Text & "%'" Else cadena = "SELECT * FROM ARTICULOS WHERE MARCA LIKE '" & TextBox6.Text & "%'" End If Dim cmd As New SqlCommand(cadena, cnn) cmd.CommandType = CommandType.Text Dim da As New SqlDataAdapter(cmd) Dim dt As New DataTable da.Fill(dt) DataGridView1.DataSource = dt End Sub Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged TextBox6.Text = "" End Sub Lic. Omar Angel Mamani Rodriguez docentes.msc@gmail.com IESTP MANUEL SEOANE CORRALES PROGRAMACIÓN DISTRIBUIDA Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click LIMPIAR() End Sub Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click Close() End Sub End Class 4. Diseñar y programar el siguiente formulario (Hacer Los cambios necesarios para que se adapte) En este formulario colocarás el Id_Articulo y al darle buscar se mostrará sus datos los que le corresponda a la tabla Articulos. Lic. Omar Angel Mamani Rodriguez docentes.msc@gmail.com