Anda di halaman 1dari 55

Cambiamos la vista Por ese nombre vw_Productos

PROCEDIMIENTO PARA COMBOX CON 2 CAMPOS

CREATE procedure usp_camposduo2 As Begin Select Cargo + ' ' + Nombre + ' ' + Apellidos as Vendedor, apellidos, nombre, cargo from Empleados End

De la clase 01 --Procedimientos Almacenados

Create Procedure usp_ListaClientes As Begin Select IdCliente,NombreCompaa,NombreContacto,Ciudad,Pas,Direccin from Clientes End

go

Create Procedure usp_ListaEmpleados As Begin Select IdEmpleado,Apellidos + ' ' + Nombre as Vendedor, Apellidos,Nombre,Cargo from Empleados End go

Exec usp_ListaEmpleados go

Create Procedure usp_ProductosNombre @Producto VarChar(100) As Begin Select * from VW_Productos Where NombreProducto LIKE @Producto + '%'

End go Exec usp_ProductosNombre 'Me'

B205-13

Imports System.Data Imports System.Data.SqlClient

<connectionStrings> <add name="cnNeptuno" connectionString="Data Source=B205-06; Initial Catalog=Neptuno;Integrated Security=True" providerName="System.Data.SqlClient"/> </connectionStrings>

Public cn As New SqlConnection("Data Source=B205-06;Initial Catalog=Neptuno;Integrated Security=True")

EMPEZAMOS LA CLASE 2 ABRIMOS FORMUYLARIO CLIENTES DE LA CLASE 1 APLICA PROCEDIMIENTOS

Public Class frmClientes Dim obj As New Neptuno Private Sub frmClientes_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load dgvclientes.DataSource = obj.EjecutarSP("usp_ListaClientes").Tables(0).DefaultView

Y EJECUTAMOS

TRABAJAREMOS AHORA CON EL FORMULARIO PRODUCTOS NOMBRES


AL ESCRIBIR UN NOMBRE DE PRODUCTO AUTOMATIMENTE LO BUSCA CARCTER X CARCTER PARA ELLOS DEBEMOS YA TENER CREADO ADD Referencia App Procedure Usp_ProductosNombre Vista VW Productos

Creamos una function dentro de la clase Neptuno Productos Nombres


Public Function ProductosNombre(ByVal prod As String) As DataTable da = New SqlDataAdapter("usp_ProductosNombre", cn) da.SelectCommand.CommandType = CommandType.StoredProcedure da.SelectCommand.Parameters.AddWithValue("@Producto", prod) dt = New DataTable da.Fill(dt)

Return dt End Function

Aadimos a la Clase de Neptuno


Public dt As New DataTable

da = New SqlDataAdapter("usp_ProductosNombre", cn) da.SelectCommand.CommandType = CommandType.StoredProcedure da.SelectCommand.Parameters.AddWithValue("@producto", prod) dt = New DataTable da.Fill(dt) Return dt

agregamos una lnea a la librera clase Neptuno Public dt As New DataTable

Instanciamos la clase Neptuno


Dim obj As New Neptuno

Resumen
Imports System.Data Imports System.Data.SqlClient Imports System.Configuration.ConfigurationManager

Public Class Neptuno Public cn As New SqlConnection(ConnectionStrings("cnNeptuno").ConnectionString) Public da As New SqlDataAdapter

Public ds As New DataSet Public dt As New DataTable 'ejecuta cualquier procedIMIENTOS Aalmacenado qeu NO tengan parametros Public Function EjecutarSP(ByVal Nombre As String) As DataSet da = New SqlDataAdapter(Nombre, cn) da.SelectCommand.CommandType = CommandType.StoredProcedure ds = New DataSet da.Fill(ds) Return ds End Function Public Function ProductosNombre(ByVal prod As String) As DataTable da = New SqlDataAdapter("usp_ProductosNombre", cn) da.SelectCommand.CommandType = CommandType.StoredProcedure da.SelectCommand.Parameters.AddWithValue("@producto", prod) dt = New DataTable da.Fill(dt) Return dt End Function End Class Public Class frmProductosNombre Dim obj As New Neptuno Private Sub frmProductosNombre_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load dgvProductos.DataSource = obj.ProductosNombre("") End Sub

Private Sub txtNombre_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtNombre.TextChanged dgvProductos.DataSource = obj.ProductosNombre(txtNombre.Text.Trim) End Sub End Class

AHORA TRABAJAREMOS CON EL FORMULARIO CAMBIANDO EL NOMBRE FORMULARIOS

FRM EMPLEADO ---- FRM PEDIDOS X EMPLEADOS

Abrimos Sql Server y Creamos vista VW PEDIDOS

Agregamos las 3 tablas / Pedidos clientes y Empleados

SELECT * FROM VW PEDIDOS

Id pedido / Fecha de entrega / Nombre compaa / Apellidos / Nombres

Crearemos otro procedimiento almacenado *****

Usp_pedidos_empleados
create procedure usp_pedidos_empleado @empleado varchar(50) as begin select * from vw_Pedidos where apellidos = @empleado end go exec usp_pedidos_empleados 'Davolio'

idpedido fechaentrega

cambiar nombre formulario Empleados x frmPedidosEmpleado ingresamos 2 controles combobox1 ---- cboEmpleados datagriwview1 ----- dgvpedidos

Procedemos a crear una funcin PedidoEmpleado en la Clase Neptuno

Public Function PedidoEmpleado(ByVal emp As String) As DataTable da = New SqlDataAdapter("usp_Pedido_Empleado", cn) da.SelectCommand.CommandType = CommandType.StoredProcedure da.SelectCommand.Parameters.AddWithValue("@empleado", emp) dt = New DataTable da.Fill(dt) Return dt End Function

Vamos al formulario Instanciamos la clase Neptuno en el frmPedidoEmpleado


Dim Obj as New Neptuno

Escribimos en el evento load del frmPedidoempleado

Private Sub frmPedidosEmpleado_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load cboempleados.DataSource = obj.EjecutarSP("usp_listaEmplado").tables(0) cboempleados.DisplayMember = "vendedor" cboempleados.ValueMember = "Apellidos" End Sub

CODIFICAMOS EN EL CONTROL CMBOEMPLEADO - EVENTO SELECTED INDEX CHANGE

Try dgvpedidos.DataSource = obj.PedidoEmpleado(cboempleados.SelectedValue. ToString) Catch ex As Exception

Ojo CORREGIDO
Private Sub frmEmpleados_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load cboempleados.DataSource = obj.EjecutarSP("usp_ListaEmpleados").Tables(0) cboempleados.DisplayMember = "Vendedor" cboempleados.ValueMember = "apellidos" 'cboempleados_SelectedIndexChanged(sender, e)

Fin FORMULARIO

Observacin
Para modificar un procedimiento almacenado
ALTER PROCEDURE [dbo].[usp_ListaEmpleados]
as Begin Select IdEmpleado,Apellidos + ' ' + Nombre as Vendedor,Apellidos, Nombre, Cargo from Empleados End

Y F5

LISTO

Crear procedimiento ups pedidos fecha


create procedure usp_Pedidos_Fechas @Fec1 DATETIME, @Fec2 DATETIME AS BEGIN SELECT * FROM VW_PEDIDOS WHERE FechaPedido BETWEEN @Fec1 and @Fec2 end go exec usp_Pedidos_Fechas '01/01/1996' , '08/08/1996'

CREAMOS UN FORMULARIO PEDIDOS FECHA

AADIMOS 2 CONTROLES DATE TIME PICKER = F1 F2 UN DATAGRIVVIEW = DGVPEDIDOSFECHA UN BOTON DE COMANDO = BTNCONSULTAR

VAMOS A LA CLASE NEPTUNO ****** Y AADIMOS - LA FUNCION - PEDIDOS FECHAS

Public Function PedidosFechas(ByVal f1 As Date, ByVal f2 As Date) As DataTable} da = New SqlDataAdapter("usp_PedidosFechas", cn) da.SelectCommand.CommandType = CommandType.StoredProcedure da.SelectCommand.Parameters.AddWithValue("@fec1", f1) da.SelectCommand.Parameters.AddWithValue("@fec2", f2) dt = New DataTable da.Fill(dt) Return dt End Function

INSTANCIAMOS LA CLASE NEPTUNO EN EL FRM PEDIDOS FECHA


Dim OBJ As New Neptuno

Formularios pedidos fecha


Dim OBJ As New Neptuno

Private Sub btnconsultar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnconsultar.Click dgvpedidosfechas.DataSource = obj.PedidosFechas(f1.Value, f2.Value) Label1.Text = "Son " & obj.PedidosFechas(f1.Value, f2.Value).Rows.Count.ToString & " Pedidos"

End Sub

Creamos vista detalles

Create procedure usp_pedidos_detallez


@idpedido int As Begin Select * from VW_Detalles Where idpedido =@idpedido End

Exec usp_pedidos_detallez 10250

Id pedido
XXXXX

nombre procuto precio unidad


XXXX XXXXX

cantidad
XXX XXX

IMPORTE

Vw detalles

Vamos a CLASE Neptuno Creamos una function llamada - function pedidos detalles -

Public Function PedidosDetalles(ByVal pedido As Integer) As DataTable da = New SqlDataAdapter("usp_Pedidos_Detalle", cn) da.SelectCommand.CommandType = CommandType.StoredProcedure da.SelectCommand.Parameters.AddWithValue("Idpedido", pedido) dt = New DataTable da.Fill(dt) Return dt End Function

Public Function PedidosDetalles(ByVal pedido As Integer) As DataTable da = New SqlDataAdapter("usp_Pedidos_Detallez", cn) da.SelectCommand.CommandType = CommandType.StoredProcedure da.SelectCommand.Parameters.AddWithValue("Idpedido", pedido) dt = New DataTable da.Fill(dt) Return dt End Function

AADIMOS OTRO DATAGRIWVIEW - DGVDETALLES

Private Sub dgvpedidosfechas_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dgvpedidosfechas.SelectionChanged Try dgvdetalles.DataSource = obj.PedidosDetalles(dgvpedidosfechas.Rows(dgvpedidosfechas.CurrentCell.RowIndex).Cells(0).Value) lbldetalle.Text = "Detalles del pedido nro >>> " & dgvpedidosfechas.Rows(dgvpedidosfechas.CurrentCell.RowIndex).Cells(0).Value Catch ex As Exception End Try

End Sub

Total pedido

create procedure usp_pedido_total @idpedido int, @total numeric(10,2) output as begin select @total = SUM(importe) from VW_detalles where idpedido =@idpedido end go declare @tot numeric (10,2) exec usp_pedido_total 10250, @tot output print @tot

ahora lo imprementamos en la CLASE

Public Function PedidoTotal(ByVal Pedido As Integer) As Double da = New SqlDataAdapter("usp_Pedidos_Total", cn) da.SelectCommand.CommandType = CommandType.StoredProcedure da.SelectCommand.Parameters.AddWithValue("@pedido", Pedido) da.SelectCommand.Parameters.Add("Total", SqlDbType.Decimal).Direction = ParameterDirection.Output da.SelectCommand.ExecuteScalar() PedidoTotal = da.SelectCommand.Parameters("@total").Value

End Function

Modifcacion

Anda mungkin juga menyukai