You are on page 1of 6

Imports System.Data Imports System.Data.

SqlClient Public Class EmpresaDA Dim oconexion As New ConexionSql Public Function actualizar(ByVal oEmpresa As Empresa) As String Dim cmd As New SqlCommand Dim r As String = "Datos Actualizados" Try With cmd .Connection = oconexion.conectar .CommandType = CommandType.Text .CommandText = "Update shippers set companyname=@nombre ,phone=@telefono where shipperid=@id" .Parameters.AddWithValue("@nombre", oEmpresa.nombre) .Parameters.AddWithValue("@telefono", oEmpresa.telefono) .Parameters.AddWithValue("@id", oEmpresa.id) oconexion.abrirConexion() .ExecuteNonQuery() oconexion.cerrarConexion()

End With Catch ex As Exception r = ex.Message End Try Return r End Function Public Function eliminar(ByVal id As Short) As String Dim cmd As New SqlCommand Dim r As String = "Empresa Eliminada" Try With cmd .Connection = oconexion.conectar .CommandType = CommandType.Text .CommandText = "delete from shippers where shipperid=@id"

.Parameters.AddWithValue("@id", id) oconexion.abrirConexion() .ExecuteNonQuery() oconexion.cerrarConexion()

End With Catch ex As Exception r = ex.Message End Try Return r End Function

Public Function agregar(ByVal oEmpresa As Empresa) As String Dim cmd As New SqlCommand Dim r As String = "Empresa Registrada" Try With cmd .Connection = oconexion.conectar .CommandType = CommandType.StoredProcedure .CommandText = "Insert shippers (companyname,phone) values (@nombre,@telefono)" .Parameters.AddWithValue("@nombre", oEmpresa.nombre) .Parameters.AddWithValue("@telefono", oEmpresa.telefono) oconexion.abrirConexion() .ExecuteNonQuery() oconexion.cerrarConexion()

End With Catch ex As Exception r = ex.Message End Try Return r End Function

Function listar() As DataTable Dim cn As New ConexionSql Dim ds As New DataSet Try Dim da As New SqlDataAdapter("Select Shipperid Codigo,Companyname Nombre,phone Telefono from shippers order by shipperid desc", cn.conectar) da.Fill(ds) Catch ex As Exception MsgBox(ex.Message) End Try Return ds.Tables(0) End Function Function buscar(ByVal codigo As Short) As Empresa Dim cmd As New SqlCommand Dim dr As SqlDataReader Dim oEmpresa As New Empresa With cmd .Connection = oconexion.conectar .CommandType = CommandType.Text .CommandText = "Select companyname,phone from shippers where SHIPPERID=@id" .Parameters.AddWithValue("@id", codigo) oconexion.abrirConexion() dr = .ExecuteReader If dr.Read Then oEmpresa.nombre = dr(0) oEmpresa.telefono = dr(1) Else oEmpresa = Nothing End If oconexion.cerrarConexion() Return oEmpresa End With End Function

Dim oNegocio As New EmpresaLN Dim cod As Short Sub botones(ByVal bNuevo As Boolean, ByVal bGuardar As Boolean, ByVal bBuscar As Boolean, ByVal bActua As Boolean, ByVal bElimina As Boolean) btnNuevo.Enabled = bNuevo btnGuardar.Enabled = bGuardar btnBuscar.Enabled = bBuscar btnActualizar.Enabled = bActua btnEliminar.Enabled = bElimina End Sub Sub limpiar() txtNom.Clear() txtTele.Clear() End Sub Sub lista() dgvLista.DataSource = oNegocio.listarEmpresa End Sub Private Sub FrmEmpresa_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load lista() botones(True, True, True, False, False) End Sub

Private Sub btnGuardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGuardar.Click Dim m As String = oNegocio.agregarEmpresa(New Empresa(txtNom.Text, txtTele.Text)) MsgBox(m) lista() End Sub

Private Sub btnBuscar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscar.Click

cod = CInt(InputBox("codigo a buscar")) Dim oempresa As New Empresa oempresa = oNegocio.buscarEmpresa(cod)

If Not oempresa Is Nothing Then txtNom.Text = oempresa.nombre txtTele.Text = oempresa.telefono botones(True, False, True, True, True) Else botones(True, True, True, False, False) txtNom.Clear() txtTele.Clear() MsgBox("CODIGO NO EXISTE") End If

End Sub

Private Sub btnNuevo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNuevo.Click limpiar() botones(True, True, True, False, False) txtNom.Focus() End Sub

Private Sub btnActualizar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnActualizar.Click Dim m As String = oNegocio.actualizarEmpresa(New Empresa(cod, txtNom.Text, txtTele.Text)) MsgBox(m) lista() limpiar() botones(True, True, True, False, False) End Sub

Private Sub btnEliminar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEliminar.Click Dim m As String = oNegocio.eliminarEmpresa(cod) MsgBox(m) lista() limpiar() botones(True, True, True, False, False)

You might also like