Consulta usando Procedimiento Almacenado

ESTRUCTURA DE LA BASE DE DATOS
create database matriculas

use matriculas
create table alumnos
(cod varchar(2) primary key,
nom varchar(10),
ape varchar(10),
dir varchar(20),
)


insert into
alumnos values ('01','Luis','Castro','Callao')insert into alumnos values ('02','Diana','Montes','Olivos')insert into alumnos values ('03','Carlos','Perez','Cercado')insert into alumnos values ('04','Sofia','Valderrama','Olivos')insert into alumnos values ('05','Miguel','Osorio','Surco')

select * from
alumnos

/*CREACION DEL PRIMER PROCEDIMIENTO ALMACENADO*/
create procedure codigos1
as
select cod
from
alumnos

/* EJECUTA PROCEDIMIENTO*/
exec
codigos1

/*CREACION DEL SEGUNDO PROCEDIMIENTO ALMACENADO*/
create procedure codigos
@xcod varchar(5)

as
select nom, ape, dir
from
alumnos
where cod = @xcod

/* EJECUTA PROCEDIMIENTO*/
exec codigos '04'


APLICACION EN VISUAL .NET


Imports System.Data.SqlClient

Public Class Form1
Dim cn As New SqlConnection
Dim cadena As String = "Data Source=TALLER-106\SQLEXPRESS; Initial Catalog=Matriculas; Integrated Security=True"

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) H...
cn.ConnectionString = cadena
Dim cmd As New SqlCommand
cmd.Connection = cn

Dim da As New SqlDataAdapter
da.SelectCommand = cmd
Dim ds As New DataSet

'CARGA LOS CODIGOS COMBOBOX1
Dim cmd1 As New SqlCommand("CODIGOS1", cn)
With cmd1
.CommandType = CommandType.StoredProcedure
Dim da1 As New SqlDataAdapter(cmd1)
Dim dt1 As New DataTable
da1.Fill(dt1)
ComboBox1.DataSource = dt1
ComboBox1.DisplayMember = "COD"
End With

End Sub


Private Sub ComboBox1_SelectedValueChanged(ByVal sender As Object, ByVal ....
Dim cmd2 As New SqlCommand("CODIGOS", cn)
TextBox1.DataBindings.Clear()
TextBox2.DataBindings.Clear()
TextBox3.DataBindings.Clear()
With cmd2
.CommandType = CommandType.StoredProcedure
.Parameters.Add(New SqlParameter("@xcod", ComboBox1.Text))

Dim da2 As New SqlDataAdapter(cmd2)
Dim dt2 As New DataTable

da2.Fill(dt2)
TextBox1.DataBindings.Add(New Binding("Text",
dt2, "NOM"))
TextBox2.DataBindings.Add(New
Binding("Text", dt2, "APE"))
TextBox3.DataBindings.Add(New
Binding("Text", dt2, "DIR"))
End With

End Sub

End Class

NOTAS:

* Recuerda modificar por tu conexión local.

Data Source=TALLER-106\SQLEXPRESS