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.

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"];  

How to define DataRelation

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);  
dtSet.Tables["tbOrderItems"].ParentRelations.Add(dtRelation);  

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 .

 
DataRelation in Ado.Net
Learn MS-SQL Development
Ado.net Interview Questions Answers
Connect SQL database using Ado.Net component from Asp.net application, learn how to use Ado.net as data access layer.
Ado.Net C# Examples | Join Asp.Net MVC Course