Clase Entidad en Linq
Imports System.Data.Linq Imports SystemImports System.Collections.Generic Imports System.ComponentModel Imports System.Data Imports System.Data.Linq.Mapping Imports System.Linq <Table(Name:="TContactenos")> _Public Class CContactenos Private _Id As Long Private _Nombres As String Private _Dni As String Private _Email As String Public Sub New() MyBase.New() End Sub <Column(Storage:="_Id", DbType:="BigInt NOT NULL", IsPrimaryKey:=True)> _ Public Property Id() As Long Get Return Me._Id End Get Set(value As Long) If (String.Equals(Me._Nombres, value) = False) Then Me._Id = value End If End Set End Property <Column(Storage:="_Nombres", DbType:="NVarChar(100)")> _ Public Property Nombres() As String Get Return Me._Nombres End Get Set(value As String) If (String.Equals(Me._Nombres, value) = False) Then Me._Nombres = value End If End Set End Property <Column(Storage:="_Dni", DbType:="NChar(8)")> _ Public Property Dni() As String Get Return Me._Dni End Get Set(value As String) If (String.Equals(Me._Dni, value) = False) Then Me._Dni = value End If End Set End PropertyEnd Class
Clase que Manejara las clases Entidades para manipular la BD (USANDO CLASE DATACONTEXT)
Imports System.Data.Linq Public Class StoredBD Inherits DataContext Public Sub New(ByVal connection As String) MyBase.New(connection) End Sub Public ReadOnly Property CContactenos() As Table(Of CContactenos) Get Return Me.GetTable(Of CContactenos)() End Get End PropertyEnd Class
En el codigo del MainPage.XAML, implementamos el evento click del boton guardar de la siguienta forma
Imports System.Data.Linq Imports System.Data Partial Public Class MainPage Inherits PhoneApplicationPage Shared contador As Long = 0 ' Constructor Public Sub New() InitializeComponent() Me.cargarBD() End Sub Private Sub btnGuardar_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles btnGuardar.Click contador += 1 Me.insertarRegistroBD(contador, txtNombres.Text, txtDni.Text, _ txtEmail.Text, txtComentario.Text) End Sub Private Sub cargarBD() Dim myBD As StoredBD = New StoredBD() Try Using myBD If Not myBD.DatabaseExists Then myBD.CreateDatabase() End If End Using Catch ex As Exception MessageBox.Show(ex.Message, "Exepcion cargarBD", MessageBoxButton.OK) Finally myBD.Dispose() End Try End Sub Private Sub insertarRegistroBD(ByVal pId As Long, ByVal pNombres As String, _ ByVal pDni As String, ByVal pEmail As String, ByVal pComentario As String) Dim myBD As StoredBD = New StoredBD() Try Dim registro As CContactenos registro = New CContactenos() With {.Id = pId, .Nombres = pNombres, _ .Dni = pDni, .Email = pEmail, .Comentario = pComentario} myBD.CContactenos.InsertOnSubmit(registro) myBD.SubmitChanges() Catch ex As Exception MessageBox.Show(ex.Message, "Exepcion cargarBD", MessageBoxButton.OK) Finally myBD.Dispose() End Try End SubEnd Class
Comentarios
Publicar un comentario