DATARELATION




SQL



CREATE DATABASE DBDEMO

USE DBDEMO

CREATE TABLE DOCENTES
(COD INT Primary key,
NOM varchar(15),
APE varchar(15),
CODESP int,
)


insert into DOCENTES values(1, 'Juan','Perez',1)
insert into DOCENTES values(2, 'Diana','Campos',1) insert into DOCENTES values(3, 'Mirian','Suarez',2) insert into DOCENTES values(4, 'David','Altamirano',3) insert into DOCENTES values(5, 'Angel','Montes',4)

Select * From DOCENTES

CREATE TABLE ESPECIALIDAD
(CODESP INT Primary key,
DES varchar(15),
)

insert into ESPECIALIDAD values(1, 'Ing. Sistemas')
insert into ESPECIALIDAD values(2, 'Administrador') insert into ESPECIALIDAD values(3, 'Contabilidad') insert into ESPECIALIDAD values(4, 'Economía')
Select * From ESPECIALIDAD




CODIGO VISUAL BASIC .NET



Imports System.Data
Imports System.Data.SqlClient

Public Class Form1
Private Sub Form1_Load

Dim cn As New SqlConnection
Dim cadena As String
cadena = "Data Source='localhost'; Initial Catalog=DBDEMO; Integrated Security=True"
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 DATOS TABLA ESPECIALIDAD
cmd.CommandText = "Select * from ESPECIALIDAD order by des"
da.Fill(ds, "ESPECIALIDAD")

DataGridView1.DataSource = ds.Tables("ESPECIALIDAD")

'CARGA DATOS TABLA DOCENTE
cmd.CommandText = "Select * from DOCENTES order by nom"
da.Fill(ds, "DOCENTES")

DataGridView2.DataSource = ds.Tables("DOCENTES")

'CREA LAS RELACIONES
Dim relacion As DataRelation
Dim principal, secundario As DataColumn

principal = ds.Tables("ESPECIALIDAD").Columns("CODESP")
secundario = ds.Tables("DOCENTES").Columns("CODESP")

relacion = New DataRelation("RELACION1", principal, secundario)

ds.Relations.Add(relacion)

DataGridView3.DataSource = ds

End Sub
End Class