0% found this document useful (0 votes)
86 views6 pages

Practica Desarrollo de Aplicaciones I Altas Bajas Consultas Cambios

This document describes the code for a menu form and several additional forms for performing CRUD operations on a SQL database using ADO.NET in Visual Basic. The menu form contains options to open forms for insert, query, delete, update, and report operations. Each operation form contains code to connect to a SQL database, build and execute the appropriate SQL statement, and handle the results.

Uploaded by

davidrey29
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
86 views6 pages

Practica Desarrollo de Aplicaciones I Altas Bajas Consultas Cambios

This document describes the code for a menu form and several additional forms for performing CRUD operations on a SQL database using ADO.NET in Visual Basic. The menu form contains options to open forms for insert, query, delete, update, and report operations. Each operation form contains code to connect to a SQL database, build and execute the appropriate SQL statement, and handle the results.

Uploaded by

davidrey29
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 6

Prctica Desarrollo de Aplicaciones I: Altas, Bajas, Consultas y Cambios en SQL utilizando ADO.

NET

FORMA DEL MENU PRINCIPAL

Public Class Form1 Private Sub MenuDelSistemaToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuDelSistemaToolStripMenuItem.Click End Sub Private Sub AltaToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AltaToolStripMenuItem.Click Dim forma2 As New Form2() forma2.MdiParent = Me forma2.Show() End Sub Private Sub SalirToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SalirToolStripMenuItem.Click Me.Close() End Sub Private Sub ConsultaToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ConsultaToolStripMenuItem.Click Dim forma3 As New Form3() forma3.MdiParent = Me forma3.Show() End Sub Private Sub BajaToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BajaToolStripMenuItem.Click Dim forma4 As New Form4() forma4.MdiParent = Me forma4.Show() End Sub Private Sub CambiosToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CambiosToolStripMenuItem.Click Dim forma5 As New Form5() forma5.MdiParent = Me forma5.Show() End Sub Private Sub ImprimirReporteToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ImprimirReporteToolStripMenuItem.Click Dim forma6 As New Form6() forma6.MdiParent = Me forma6.Show() End Sub End Class

FORMA ALTAS
Imports System.Data.OleDb Public Class Form2 Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click TextBox1.Clear() TextBox2.Clear() TextBox3.Clear() TextBox4.Clear() TextBox5.Clear() End Sub Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click Me.Close() End Sub ' Copia el contenido de un DataReader en un DataTable Private Function ConvDeDataReader(ByVal DR As IDataReader) As DataTable ' Crea un objeto DataTable que contenga los resultados Dim Tbl As New DataTable() ' Ajusta el nombre de las columnas del DataTable For i As Integer = 0 To DR.FieldCount - 1 Tbl.Columns.Add(DR.GetName(i)) Next ' Crea un arreglo ("array") que contenga los valores de la lnea Dim Linea As Object() = New Object(DR.FieldCount - 1) {} ' Lee todos los registros While DR.Read() ' Inserta los valores del registro actual DR.GetValues(Linea) ' Agrega el registro al objeto DataTable Tbl.Rows.Add(Linea) End While Return Tbl

End Function Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim OleDbConnection1 As New OleDbConnection() OleDbConnection1.ConnectionString = "Provider=SQLOLEDB;Data Source=DAVID;Integrated Security=SSPI;Initial Catalog=alumnosutsc" If TextBox1.Text <> "" AndAlso TextBox2.Text <> "" AndAlso TextBox3.Text <> "" AndAlso TextBox4.Text <> "" AndAlso TextBox5.Text <> "" Then OleDbConnection1.Open() Dim existe As Boolean Try Dim Cmd2 As New OleDbCommand("select matricula from alumno where(matricula='" + TextBox1.Text & "')", OleDbConnection1) Dim alumnos As OleDbDataReader alumnos = Cmd2.ExecuteReader() existe = alumnos.Read() alumnos.Close() ' Crea el objeto asociado al comando SQL If existe = False Then Dim Cmd As New OleDbCommand((((("insert into alumno (matricula,nombre,calificacion1,calificacion2,calificacion3) values ('" + TextBox1.Text & "','") + TextBox2.Text & "','") + TextBox3.Text & "','") + TextBox4.Text & "','") + TextBox5.Text & "')", OleDbConnection1) Cmd.ExecuteNonQuery() Else MessageBox.Show("la matricula ya existe") End If Finally ' Cierra la conexin OleDbConnection1.Close() End Try Else MessageBox.Show("Faltan campos por llenar") End If TextBox1.Clear() TextBox2.Clear() TextBox3.Clear() TextBox4.Clear() TextBox5.Clear() End Sub End Class

FORMA CONSULTAS

Imports System.Data.OleDb Public Class Form3 ' Copia el contenido de un DataReader en un DataTable Private Function ConvDeDataReader(ByVal DR As IDataReader) As DataTable ' Crea un objeto DataTable que contenga los resultados Dim Tbl As New DataTable() ' Ajusta el nombre de las columnas del DataTable For i As Integer = 0 To DR.FieldCount - 1 Tbl.Columns.Add(DR.GetName(i)) Next ' Crea un arreglo ("array") que contenga los valores de la lnea Dim Linea As Object() = New Object(DR.FieldCount - 1) {} ' Lee todos los registros While DR.Read() ' Inserta los valores del registro actual DR.GetValues(Linea) ' Agrega el registro al objeto DataTable Tbl.Rows.Add(Linea) End While Return Tbl End Function Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim OleDbConnection1 As New OleDbConnection() OleDbConnection1.ConnectionString = "Provider=SQLOLEDB;Data Source=DAVID;Integrated Security=SSPI;Initial Catalog=alumnosutsc" ' Abre la conexin OleDbConnection1.Open() Try

' Crea el objeto asociado al comando SQL Dim Cmd As New OleDbCommand("select * from alumno", OleDbConnection1) Dim alumnos As OleDbDataReader ' Ejecuta el comando y coloca el resultado en el DataReader alumnos = Cmd.ExecuteReader() Try ' Agrega los valores y los coloca en un DataTable Dim Tbl As DataTable = ConvDeDataReader(alumnos) ' Muestra el DataTable en el grid DataGridView1.DataSource = Tbl Finally ' Cierra el DataReader alumnos.Close() End Try Finally ' Cierra la conexin OleDbConnection1.Close() End Try End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Me.Close() End Sub End Class

FORMA BAJAS

Imports System.Data.OleDb Public Class Form4 Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click TextBox1.Clear() End Sub Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click Me.Close() End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim OleDbConnection1 As New OleDbConnection() OleDbConnection1.ConnectionString = "Provider=SQLOLEDB;Data Source=DAVID;Integrated Security=SSPI;Initial Catalog=alumnosutsc" If TextBox1.Text <> "" Then OleDbConnection1.Open() Dim existe As Boolean Try Dim Cmd2 As New OleDbCommand("select matricula from alumno where(matricula='" + TextBox1.Text & "')", OleDbConnection1) Dim alumnos As OleDbDataReader alumnos = Cmd2.ExecuteReader() existe = alumnos.Read() alumnos.Close() ' Crea el objeto asociado al comando SQL If existe = True Then Dim Cmd As New OleDbCommand("delete from alumno where(matricula ='" + TextBox1.Text & "')", OleDbConnection1) Cmd.ExecuteNonQuery() Else MessageBox.Show("la matricula NO existe") End If Finally ' Cierra la conexin OleDbConnection1.Close() End Try Else MessageBox.Show("El campo matricula esta vacio") End If TextBox1.Clear() End Sub End Class

FORMA CAMBIOS
Imports System.Data.OleDb Public Class Form5 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim OleDbConnection1 As New OleDbConnection() OleDbConnection1.ConnectionString = "Provider=SQLOLEDB;Data Source=DAVID;Integrated Security=SSPI;Initial Catalog=alumnosutsc"

If TextBox1.Text <> "" AndAlso (RadioButton1.Checked = True OrElse RadioButton2.Checked = True OrElse RadioButton3.Checked = True) AndAlso TextBox2.Text <> "" Then OleDbConnection1.Open() Dim existe As Boolean Try Dim Cmd2 As New OleDbCommand("select matricula from alumno where(matricula='" + TextBox1.Text & "')", OleDbConnection1) Dim alumnos As OleDbDataReader alumnos = Cmd2.ExecuteReader() existe = alumnos.Read() alumnos.Close() If existe = True Then ' Crea el objeto asociado al comando SQL If RadioButton1.Checked = True Then Dim Cmd As New OleDbCommand(("update alumno set calificacion1 ='" + TextBox2.Text & "' where(matricula ='") + TextBox1.Text & "')", OleDbConnection1) Cmd.ExecuteNonQuery() End If If RadioButton2.Checked = True Then Dim Cmd As New OleDbCommand(("update alumno set calificacion2 ='" + TextBox2.Text & "' where(matricula ='") + TextBox1.Text & "')", OleDbConnection1) Cmd.ExecuteNonQuery() End If If RadioButton3.Checked = True Then Dim Cmd As New OleDbCommand(("update alumno set calificacion3 ='" + TextBox2.Text & "' where(matricula ='") + TextBox1.Text & "')", OleDbConnection1) Cmd.ExecuteNonQuery() End If Else MessageBox.Show("la matricula NO existe") End If Finally ' Cierra la conexin OleDbConnection1.Close() End Try Else MessageBox.Show("faltan campos por llenar") End If TextBox1.Clear() RadioButton1.Checked = False RadioButton2.Checked = False RadioButton3.Checked = False TextBox2.Clear() End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click TextBox1.Clear() RadioButton1.Checked = False RadioButton2.Checked = False RadioButton3.Checked = False TextBox2.Clear() End Sub Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click Me.Close() End Sub End Class

You might also like