Saturday, December 17, 2011

Data set with Data Relation in asp.net

Here i will shown how to give the primary key and foreign key to tables .Data set having collection of data table and data relations.while generating relations in the data set at least we have to store two tables .while creating the tables we have to create tables with Primary key constraint and with foreign key constraint.The p.k constraint column table is master table and F.K constraint column table is child table.The P.K constraint column automatically controls F.K constraint column values

Data Relation:This class is used to generate relation in the database .This class takes constructor arguments
1.Relation name
2.Primary Key Constraint Column
3.Foreign Key Constraint Column
Example:
Using System.Data.Oledb;

OledbConnection cn=New OledbConnection("Provider=msdaora.1;user id=test;pasword=test");
OledbDataAdapter ad1;
OledbDataAdapter ad2;
Dataset ds=new Dataset();
DataRelation dr;
//under button click event
{
ad1=New OledbDataAdapter("select*from Employee",cn);
ad1=New OledbDataAdapter("select*from Department",cn);
ad1.Fill(ds,"department");
ad2.Fill(ds,"Employee");
DataColumn PK=ds.Tables["Employee"].columns["empid"];
DataColumn FK=ds.Tables["Department"].columns["empidid"];
dr=New DataRealtion("re1",PK,FK);
ds.Realtions.Add(dr);
gvmain.Datasource=ds;
}

No comments:

Bel