Como vemso consta de 5 campos de los cuales vemos que la clave principal es el campo codigo y ademas esta presenta la propiedad identity por lo que no debemos preocuparnos de realizar las operaciones con ese campo, bueno el formulario tendra mas la siguiente apariencia
Como vemos la aplicacion consta de Tres partes Datos, Navegacion y Edicion de Registros con sus botones correspondientes.el codigo necesario para ejecutar nuestra aplicacion es la que sigue
Imports System.Data.SqlClient
Imports System.Data.SqlTypes
Public Class Form1
Inherits System.Windows.Forms.Form
Public cn As New SqlConnection
Public da As SqlDataAdapter
Public ds As DataSet
Public enlacebase As BindingManagerBase
Public enlace As Binding
Public cmd As SqlCommandBuilder
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
cn.ConnectionString = "data source=Familia;initial catalog=Colegio;integrated security=true;"
da = New SqlDataAdapter("select * from alumnos", cn)
cmd = New SqlCommandBuilder(da)
cn.Open()
ds = New DataSet
da.Fill(ds, "alumnos")
cn.Close()
enlace = New Binding("text", ds, "alumnos.codigo")
TextBox1.DataBindings.Add(enlace)
enlace = Nothing
enlace = New Binding("text", ds, "alumnos.nombre")
TextBox2.DataBindings.Add(enlace)
enlace = Nothing
enlace = New Binding("text", ds, "alumnos.grado")
TextBox3.DataBindings.Add(enlace)
enlace = Nothing
enlace = New Binding("text", ds, "alumnos.seccion")
TextBox4.DataBindings.Add(enlace)
enlace = Nothing
enlace = New Binding("text", ds, "alumnos.IdSancion")
TextBox5.DataBindings.Add(enlace)
enlace = Nothing
Me.enlacebase = Me.BindingContext(ds, "alumnos")
Call mostrar_pos()
End Sub
Public Sub mostrar_pos()
Label6.Text = "Registro " & Me.enlacebase.Position + 1 & " de " & Me.enlacebase.Count
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Try
If Me.enlacebase.Position = Me.enlacebase.Count - 1 Then
Label7.Text = "Ya estamos en el Ultimo Registro"
Else
Me.enlacebase.Position += 1
Call mostrar_pos()
Label7.Text = ""
End If
Catch ex As Exception
MessageBox.Show(ex.Message.ToString)
End Try
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Try
If Me.enlacebase.Position = 0 Then
Label7.Text = "Ya estamos en el Primer Registro"
Else
Me.enlacebase.Position -= 1
Call mostrar_pos()
Label7.Text = ""
End If
Catch ex As Exception
MessageBox.Show(ex.Message.ToString)
End Try
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
Me.enlacebase.Position = 0
Call mostrar_pos()
Label7.Text = "Estamos en el Primer Registro"
Catch ex As Exception
End Try
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Me.enlacebase.Position = Me.enlacebase.Count - 1
Call mostrar_pos()
Label7.Text = "Estamos en el Ultimo Registro"
End Sub
Private Sub btLimpiar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btLimpiar.Click
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
TextBox4.Text = ""
TextBox5.Text = ""
TextBox1.Focus()
End Sub
Private Sub btInsertar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btInsertar.Click
Dim fila As DataRow
fila = ds.Tables("alumnos").NewRow
fila("Nombre") = TextBox2.Text
fila("Grado") = TextBox3.Text
fila("Seccion") = TextBox4.Text
fila("IdSancion") = TextBox5.Text
Label7.Text = "Se ha insertado un registro nuevo." & vbCrLf _
& "Pulsa actualizar si deseas guardar los cambios."
Me.ds.Tables("alumnos").Rows.Add(fila)
End Sub
Private Sub btActualizar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btActualizar.Click
Try
cn.Open()
da.Update(ds.Tables("alumnos"))
ds.Clear()
da.Fill(ds.Tables("alumnos"))
cn.Close()
Call mostrar_pos()
Label7.Text = "EL registro se Grabo correctamente"
Catch ex As Exception
MsgBox(ex.Message.ToUpper)
End Try
End Sub
Private Sub btBorrar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btBorrar.Click
Me.enlacebase.RemoveAt(Me.enlacebase.Position)
Label7.Text = "Para Borrar registro de la BBDD presione Actualizar"
Call mostrar_pos()
End Sub
Private Sub btSalir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btSalir.Click
Me.Close()
End Sub
End Class
se que es una aplicacion muy sencilla pero nos enseña a trabajar con doferentes objetos , bueno la aplicacion ejecutada es la que sigue

Bueno hasta otra proxima entrega.
Luis Ramirez G
net Developer Specialist
DCE2005 2 estrellas

No hay comentarios:
Publicar un comentario