"How to use DataRelation class in windows appliacation"
In this we will see what is datarelation class. How we can use it. what are its uses. Code to show the overall steps to be done in this.
DataRelation class can be used to automatically generate and apply ForeignkeyConstraints. It allows navigating disconnected data using defined relationship between them.
It can be defined as:
DataRelation dr = new DataRelation("NameofDataRelation",Parentcol,childcol);
DataRelation rel = new DataRelation("Name",parentcol,childcol,false);
Here false means constraints object is not generated. True indicates constraints object is generated.
It is added to dataset as:
A DataRelation represents a parent/child relationship between two Datatabel objects In the same DataSet. A relation is set between two tables based on matching columns in the parent and child table. The matching columns of both the table have same type. When the DataRelation object is created , it assists to enforce some constraints on the relationship between columns like a unique constraints.
Consider an example. We have two tables Employee and Employee1. Employee table has 4 columns Emp_id, Emp_name, Emp_dept, Emp_salary and Employee1 table has columns Emp_id, Emp_contactno, Emp_address. We have to create relationship between these two tables.
Emp_id is primary key in Employee table and foreign key in employee1 table.
string connectionstring = "server=sugandha;initial catalog = dotnet;uid = sa;pwd= sugandha";
SqlConnection con = new SqlConnection(connectionstring);
SqlDataAdapter adpt = new SqlDataAdapter("select * from Employee", con);
DataSet ds = new DataSet();
SqlDataAdapter adpt1 = new SqlDataAdapter("select * from Employee1", con);
DataRelation dr = new DataRelation("Employeedetail",ds.Tables["Employee"].Columns["Emp_id"],ds.Tables["Employee1"].Columns["Emp_id"],true);
text_eid.DataBindings.Add("Text", ds.Tables["Employee"], "emp_id");
text_empname.DataBindings.Add("Text", ds.Tables["Employee"], "emp_name");
text_empdept.DataBindings.Add("Text", ds.Tables["Employee"], "emp_dept");
text_empsal.DataBindings.Add("Text", ds.Tables["Employee"], "emp_salary");
dataGridView1.DataSource = ds.Tables["Employee"];
dataGridView1.DataMember = "Employeedetail";
In the design page we have 4 labels, 4 textbox and a datagridview.
Textboxes shows the value from employee table. And datagridview shows value of employeedetail relation we have created.