Sunteți pe pagina 1din 18

Asignatura

Taller Punto Net


Código 800PN04 y 810PN04

11-08-2016 Preparador por: Freddy Villarroel Young 1


Objetivos Específicos
• Modelamiento y diseño de sistemas.
• Técnicas y Herramientas de
programación, sistemas operativos y redes.
• Manejo de Herramientas de Base de Datos.

11-08-2016 Preparador por: Freddy Villarroel Young 2


Unidades
• Lenguaje Visual Basic
• Windows Forms
• Ado .NET

11-08-2016 Preparador por: Freddy Villarroel Young 3


Base de Datos
• SQL Server
• Microsoft SQL Server Management Studio

Explorador de
Objetos
Editor SQL

Propiedades

11-08-2016 Preparador por: Freddy Villarroel Young 4


Base de Datos
• Crear Base de Datos
– create database testdata;
– use testdata;
• Crear tabla
– drop table dbo.products;
– create table dbo.products
– (ID int PRIMARY KEY NOT NULL,
– Name varchar(25) NOT NULL,
– Price money NULL,
– Descr text NULL);
• Insertar y leer
– insert into dbo.products values (1,'Nombre', 3000, 'Descripción');
– select * from dbo.products

11-08-2016 Preparador por: Freddy Villarroel Young 5


Windows Forms
• Windows Forms

11-08-2016 Preparador por: Freddy Villarroel Young 6


Windows Forms
• Formulario Windows Form
• Data Grid View sin conexión a datos

11-08-2016 Preparador por: Freddy Villarroel Young 7


Windows Forms
• Colección

11-08-2016 Preparador por: Freddy Villarroel Young 8


Windows Forms
• Inserción de filas con el Botón

Dim cntfilas As Integer = DGV.RowCount


Dim currfila As Integer = DGV.RowCount - 1

DGV.Rows.Add()
currfila = DGV.RowCount - 1
Contador de
DGV.Item(0, currfila).Value = NextLineNbr()
Líneas
DGV.Item(1, currfila).Selected = True

11-08-2016 Preparador por: Freddy Villarroel Young 9


Windows Form
• Actualizar datos
Try

descr = "Conectando a la Base de Datos..."


Const conexionstring As String = "Data Source=RLC29-PC1;Initial Catalog=demoNet;Integrated
Security=True" Conexión
Dim conexion As New SqlClient.SqlConnection(conexionstring)
conexion.Open()

Dim insertsql As New SqlClient.SqlCommand()


insertsql.Connection = conexion
Sentencia
descr = "Insertando registros..."
For fila = 0 To DGV.RowCount - 1

'Insert
insertsql.CommandText = "insert into dbo.demotable values (" & Val(DGV(0, fila).Value) & ",'" &
DGV(1, fila).Value & "')" Inserción
insertsql.ExecuteNonQuery()

Next
conexion.Close()

Catch ex As Exception
MessageBox.Show("Error en paso: " & descr & " " & ex.Message & " " & ex.ToString)
End Try

11-08-2016 Preparador por: Freddy Villarroel Young 10


Ejercicio
• Se requiere actualizar una tabla en una BDD mediante el
objeto DataGridView.
• El objeto debe estar desvinculado de la BDD.
• Implementar 2 botones
– Insertar fila: agrega una fila en el objeto
DataGridView
– Guardar: escribe los registros en la BDD
• Se deben utilizar las clases para conexión, adaptador, dataset
y comando SQL.

11-08-2016 Preparador por: Freddy Villarroel Young 11


Programa
Public Class Form1

Function NextLineNbr() As Integer

REM Determina el valor siguiente de la llave

Dim i As Integer = 0
Dim nextnum As Integer = 1

For i = 1 To DGV.RowCount
If DGV.Item(0, i - 1).Value > nextnum Then
nextnum = DGV.Item(0, i - 1).Value
End If
Next

Return nextnum + 1
End Function

11-08-2016 Preparador por: Freddy Villarroel Young 12


Programa (¼)
Private Sub btInsert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
btInsert.Click

Dim cntfilas As Integer = DGV.RowCount


Dim currfila As Integer = DGV.RowCount - 1

DGV.Rows.Add()
currfila = DGV.RowCount - 1
DGV.Item(0, currfila).Value = NextLineNbr()
DGV.Item(0, currfila).ReadOnly = True

DGV.Item(1, currfila).Selected = True

End Sub

11-08-2016 Preparador por: Freddy Villarroel Young 13


Programa (2/4)
Private Sub btSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btSave.Click
Dim descr As String = ""
Dim sqltext As String = ""
REM Inicio de sentencias protegidas
Try
descr = "Conectando a la Base de Datos..."
REM string de Conexión generado al incorporar una conexión a Base de Datos en el Proyecto
Const conexionstring = "Data Source=RLC-23-PC15\SQLEXPRESS;Initial Catalog=testdata;Integrated
Security=True"
REM Instancia la conexión a la Base de Datos
Dim conexion As New SqlClient.SqlConnection(conexionstring)
REM Abre la conexión
conexion.Open()
REM Instancia la clase de comandos de SQL
Dim insertsql As New SqlClient.SqlCommand()

REM Habilita la conexión


11-08-2016 Preparador por: Freddy Villarroel Young 14
insertsql.Connection = conexion
Programa (¾)
REM Habilita la conexión
insertsql.Connection = conexion
descr = "Insertando registros..."
For fila = 0 To DGV.RowCount - 1
REM Arma un string con la sentencia Insert
sqltext = "insert into dbo.products values ("
sqltext = sqltext & Val(DGV(0, fila).Value) & ","
sqltext = sqltext & "'" & DGV(1, fila).Value & "'" & ","
sqltext = sqltext & Val(DGV(2, fila).Value) & ","
sqltext = sqltext & "'" & DGV(3, fila).Value & "'" & ")"
REM Asigna la sentencia a la propiedad
insertsql.CommandText = sqltext
REM Ejecuta la sentencia
insertsql.ExecuteNonQuery()
Next

11-08-2016
REM Cierra la conexión Preparador por: Freddy Villarroel Young 15
Programa (4/4)

REM Cierra la conexión


conexion.Close()

Catch ex As Exception
MessageBox.Show("Error en paso: " & descr & " " & ex.Message & " " & ex.ToString)
End Try

REM El bloeque Try-Catch permite el control de los errores y evita que el programa se caiga

End Sub
End Class

11-08-2016 Preparador por: Freddy Villarroel Young 16


Taller Punto Net (.NET)

• Fin de la presentación
• Muchas gracias

11-08-2016 Preparador por: Freddy Villarroel Young 17


Taller Punto Net (.NET)
• Muchas gracias

11-08-2016 Preparador por: Freddy Villarroel Young 18

S-ar putea să vă placă și