Learn ADO.Net DataRelation in C#, how to use DataRelation with Ado.net Tables

DataRelation Class in Ado.Net

Datarelation is basically an ado.net object that define relationships between two datatables based on two columns value, like how we define relationship in sql table using primary key and foreign key, for example if we have two tables in sql database tbOrder and tbOrderItems. we can define one to many relations, like for each order Id there can be multiple order items, so we can write tbOrder.orderId= tbOrderItems.orderId.

How to define DataRelation

DataRelation class is a part of disconnected architecture in the .NET framework. It comes under System.Data namespace. DataRelation represents a relationship between DataTable based on matching column.

DataRelation dtRelation;  
DataColumn orderCol = dtSet.Tables["tbOrder"].Columns["orderId"];  
DataColumn oItemCol = dtSet.Tables["tbOrderItems"].Columns["orderId"];  

As you can see in above code, we have declare DataRelation object and we have two columns from two different ado.net tables

Now we create a new instance of datarelation object with name and two data columns.

dtRelation = new DataRelation("orderRelation", orderCol, oItemCol);  

Note, when we define any relation, both columns must have same data type.

Hope this was helpful to make you understand the datarelation object in ado.net .

learn ado.net
DataRelation in Ado.Net
Ado.net Interview Questions Answers

Ado.Net C# Examples | Join Asp.Net MVC Course