Kamis, 16 Desember 2010

How to add relations between tables in a Dataset

Imports System.Data.SqlClient
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim connetionString As String
        Dim connection As SqlConnection
        Dim command As SqlCommand
        Dim adapter As New SqlDataAdapter
        Dim ds As New DataSet
        Dim firstSql As String
        Dim secondSql As String
  connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password"
  firstSql = "Your First SQL Statement Here"
  secondSql = "Your Second SQL Statement Here"
        connection = New SqlConnection(connetionString)
            command = New SqlCommand(firstSql, connection)
            adapter.SelectCommand = command
            adapter.Fill(ds, "Table1")

            adapter.SelectCommand.CommandText = secondSql
            adapter.Fill(ds, "Table2")

            'creating data relations
            Dim relation As DataRelation
            Dim table1Column As DataColumn
            Dim table2Column As DataColumn
            'retrieve column
            table1Column = ds.Tables("Table1").Columns(0)
            table2Column = ds.Tables("table2").Columns(0)
            'relating tables
            relation = New DataRelation("relation", table1Column, table2Column)
            'assign relation to dataset

            MsgBox("Data relation completed")
        Catch ex As Exception
            MsgBox("Can not open connection ! ")
        End Try
    End Sub
End Class

Tidak ada komentar:

Posting Komentar