10
COMPUTACIÓN E INFORMÁTICA PROGRAMACIÓN Autor: Vides Arroyo Galindo E-mail: [email protected] Página 1 DESARROLLO DE APLICACIÓN DE ESCRITORIO EN VISUAL.NET DISEÑO DE BASE DE DATOS EN SQL SERVER 2008 MODELO DE ENTIDAD RELACIÓN

Programacion 3 capas en Visual.Net

Embed Size (px)

DESCRIPTION

Programación en tres capas y conexión a datos a un servidor SQL Server

Citation preview

Page 1: Programacion 3 capas en Visual.Net

COMPUTACIÓN E INFORMÁTICA – PROGRAMACIÓN

Autor: Vides Arroyo Galindo E-mail: [email protected] Página 1

DESARROLLO DE APLICACIÓN DE ESCRITORIO EN

VISUAL.NET

DISEÑO DE BASE DE DATOS EN SQL SERVER 2008

MODELO DE ENTIDAD RELACIÓN

Page 2: Programacion 3 capas en Visual.Net

COMPUTACIÓN E INFORMÁTICA – PROGRAMACIÓN

Autor: Vides Arroyo Galindo E-mail: [email protected] Página 2

PROCEDIMIENTOS ALMACENADOS

PROGRAMACIÓN EN BASIC.NET Crear un proyecto nuevo de tipo Aplicación de Windows Form.(resultado)

Guardar el proyecto.

Page 3: Programacion 3 capas en Visual.Net

COMPUTACIÓN E INFORMÁTICA – PROGRAMACIÓN

Autor: Vides Arroyo Galindo E-mail: [email protected] Página 3

CAPA ENTIDAD

Agregar un proyecto

Clic en Archivo/Agregar nuevo proyecto/Biblioteca de clases

Asigne el nombre con CapaEntidad y crear la clases para las tablas principales de

la base de datos.

Clase alumnoCE

Public Class AlumnoCE

Private _dni As String

Private _apellido As String

Private _nombres As String

Private _fecha_nac As Date

Private _idespe As String

Public Property dni() As String

Get

Return _dni

End Get

Set(ByVal value As String)

If value.Trim.Length = 8 Then

_dni = value

Else

Throw New Exception("Ingrese 8 dígitos")

End If

End Set

End Property

Public Property apellidos() As String

Get

Page 4: Programacion 3 capas en Visual.Net

COMPUTACIÓN E INFORMÁTICA – PROGRAMACIÓN

Autor: Vides Arroyo Galindo E-mail: [email protected] Página 4

Return _apellido

End Get

Set(ByVal value As String)

If value.Trim.Length > 3 Then

_apellido = value

Else

Throw New Exception("Ingrese 3 letras como mínimo en

apellidos")

End If

End Set

End Property

Public Property nombres() As String

Get

Return _nombres

End Get

Set(ByVal value As String)

_nombres = value

End Set

End Property

Public Property fechanac() As Date

Get

Return _fecha_nac

End Get

Set(ByVal value As Date)

If value.Year + 5 < (Date.Now.Year) Then

_fecha_nac = value

Else

Throw New Exception("Ingrese fecha de nacimiento mínimo 6

años")

End If

End Set

End Property

Public Property idespecialidad() As String

Get

Return _idespe

End Get

Set(ByVal value As String)

If value.Trim.Length = 3 Then

_idespe = value

Else

Throw New Exception("Vuelve a seleccionar la

especialidad")

End If

End Set

End Property

End Class

Page 5: Programacion 3 capas en Visual.Net

COMPUTACIÓN E INFORMÁTICA – PROGRAMACIÓN

Autor: Vides Arroyo Galindo E-mail: [email protected] Página 5

CAPA ACCESO A DATOS

Agregar un proyecto

Clic en Archivo/Agregar nuevo proyecto/Biblioteca de clases

Asigne el nombre como CapaDatos y crear las siguientes clases

Agregar referencia a la CapaEntidad: Clic derecho en el proyecto CapaDatos

Page 6: Programacion 3 capas en Visual.Net

COMPUTACIÓN E INFORMÁTICA – PROGRAMACIÓN

Autor: Vides Arroyo Galindo E-mail: [email protected] Página 6

Seleccione la CapaEntidad en la pestaña Proyectos y clic Aceptar

Page 7: Programacion 3 capas en Visual.Net

COMPUTACIÓN E INFORMÁTICA – PROGRAMACIÓN

Autor: Vides Arroyo Galindo E-mail: [email protected] Página 7

Clase conexión:

Clase consulta:

Clase alumno:

Imports CapaEntidad

Imports System.Data.SqlClient

Imports System.Data

Public Class alumnoCD

Private _con As New conexion

Private _com As New SqlCommand

Sub New()

_com.Connection = _con.abrir

End Sub

Public Function grabar_alumno(ByVal objalu As AlumnoCE) As String

_com.Parameters.Clear()

_com.Connection = _con.abrir

Page 8: Programacion 3 capas en Visual.Net

COMPUTACIÓN E INFORMÁTICA – PROGRAMACIÓN

Autor: Vides Arroyo Galindo E-mail: [email protected] Página 8

_com.CommandType = CommandType.StoredProcedure

_com.CommandText = "nuevoalumno"

_com.Parameters.Add(New SqlParameter("@dni", objalu.dni))

_com.Parameters.Add(New SqlParameter("@nombres", objalu.nombres))

_com.Parameters.Add(New SqlParameter("@apellidos",

objalu.apellidos))

_com.Parameters.Add(New SqlParameter("@fecha_nac",

objalu.fechanac))

_com.Parameters.Add(New SqlParameter("@idespe",

objalu.idespecialidad))

Dim nroexiste As New SqlParameter("@existe", 0)

nroexiste.Direction = ParameterDirection.Output

_com.Parameters.Add(nroexiste)

_com.ExecuteNonQuery()

_con.cerrar()

If _com.Parameters("@existe").Value = 0 Then

Return "Se Actualizó los datos"

Else

Return "Se Insertó los datos"

End If

End Function

End Class

CAPA LÓGICA DE NEGOCIOS

Agregar un proyecto con nombre de CapaLogicaNegocios

Hacer referencia a Capadatos y Capaentidad. Y crear la clase AlumnoLN

Page 9: Programacion 3 capas en Visual.Net

COMPUTACIÓN E INFORMÁTICA – PROGRAMACIÓN

Autor: Vides Arroyo Galindo E-mail: [email protected] Página 9

PROYECTO PRINCIPAL.

DISEÑO DEL FORMULARIO

CÓDIGO EN EL FORMULARIO: Imports CapaEntidad

Imports CapaLogicaNegocios

Public Class frmalumno

Dim objalumno As AlumnoCE

Dim objalumnolognegocio As AlumnoLN

Private Sub frmalumno_Load(ByVal sender As System.Object, ByVal e As

System.EventArgs) Handles MyBase.Load

objalumno = New AlumnoCE

objalumnolognegocio = New AlumnoLN

dgalumno.DataSource = objalumnolognegocio.verdata("select*from

alumno")

Utilitarios.LlenarCombo(cbespecialidad, "select*from

especialidad", "id", "nomespe")

End Sub

Private Sub btgrabar_Click(ByVal sender As System.Object, ByVal e As

System.EventArgs) Handles btgrabar.Click

Try

objalumno.dni = txtdni.Text

objalumno.nombres = txtnombres.Text

objalumno.apellidos = txtapellidos.Text

objalumno.fechanac = dtpfechanac.Value.Date.Date

objalumno.idespecialidad = cbespecialidad.SelectedValue

Page 10: Programacion 3 capas en Visual.Net

COMPUTACIÓN E INFORMÁTICA – PROGRAMACIÓN

Autor: Vides Arroyo Galindo E-mail: [email protected] Página 10

objalumnolognegocio.NUEVO_ALUMNO(objalumno)

dgalumno.DataSource =

objalumnolognegocio.verdata("select*from alumno")

Catch ex As Exception

MessageBox.Show(ex.Message)

End Try

End Sub

End Class

CLASE UTILITARIO:

Imports CapaLogicaNegocios

Public Class Utilitarios

Public Shared Sub LlenarCombo(ByVal CB As ComboBox, ByVal sql As

String, ByVal valor As String, ByVal nombre As String)

Dim objal As New AlumnoLN

CB.DataSource = objal.verdata(sql)

CB.DisplayMember = nombre

CB.ValueMember = valor

End Sub

End Class

RESULTADO