How to add relations between tables in a Dataset

The DataSet is a container that holds a DataTableCollection and a DataRelationCollection. The DataRelationCollection, accessible through the DataSet.Relations property, allows us to establish relationships between DataTable objects within the DataSet. By creating parent-child data relations using the DataRelation object, we can define connections between different tables.

DataRelation object

The DataRelation object enables us to specify how columns from different tables are related to each other. We can establish relationships by identifying one or more columns that serve as the basis for the relationship. It is important to note that the columns involved in the DataRelation should have identical data types. This ensures compatibility and consistency in the relationship between the parent and child tables.

Hierarchical connections within the DataSet

By defining these relationships, we can establish hierarchical connections within the DataSet. For example, we can define a parent table representing categories and a child table representing products. The DataRelation object would specify which columns in each table establish the relationship between categories and products.

Full Source VB.NET
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) Try connection.Open() command = New SqlCommand(firstSql, connection) adapter.SelectCommand = command adapter.Fill(ds, "Table1") adapter.SelectCommand.CommandText = secondSql adapter.Fill(ds, "Table2") adapter.Dispose() command.Dispose() connection.Close() '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 ds.Relations.Add(relation) MsgBox("Data relation completed") Catch ex As Exception MsgBox("Can not open connection ! ") End Try End Sub End Class
connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password"

Conclusion

DataSet's DataRelationCollection allows us to create parent-child relationships between DataTables. The DataRelation object facilitates the connection by specifying the related columns in different tables, ensuring that the data types match. This capability allows for the establishment of hierarchical structures within the DataSet and enhances data integrity and consistency.