Groups | Blog | Home
all groups > vb.net data > july 2004 >

vb.net data : 1 dataset for 1 table, will retain RELATIONSHIP of tables in database


Jerome Tan
7/10/2004 4:46:36 PM
Hi there,

If I make 1 dataset and 1 data adapter for each table, how do I retain the
relationship the tables have in the database?

Thanks.

Regards/
Jerome

mohamed.mossad NO[at]SPAM egdsc.microsoft.com
7/13/2004 2:53:37 PM
Hi
If you are going to use the one dataset for each datatable, you should
build your own mechanism to enforce the relationships between tables
(integrity constrains). This would be too much overhead as you will be
building a major function of any DBMS. While you can just all the
datatables to the same DataSet and use the dataRelation objects to create
relations between tables into your dataset. Basically you can reconstruct
the whole set of constrains you have in your original database in the
dataset object that you will use in your database.
You would define it something like that
DataRelation rel=new
DataRelation("myRelation",myDataTable1.TableName,myDataTable2.TableName,myDa
taTable1.Columns["Brand Id"],myDataTable2.Columns["Brand Id"],false);
Then add it to your dataset
you car read more about datarelations on this link on MSDN

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemdatadatarelationclasstopic.asp
and here you are a snippet that I had

DataColumn[] key1=new DataColumn[2];
DataColumn[] key2=new DataColumn[2];
DataColumn[] PK2=new DataColumn[5];
key1[0]=myDataTable1.Columns["Brand Id"];
key1[1]=myDataTable1.Columns["Model"];
myDataTable1.PrimaryKey=key1;

// key2[0]=ds.ProductLocation.BrandIdColumn;
// key2[1]=ds.ProductLocation.ModelColumn;
PK2[0]=myDataTable2.Columns["Brand Id"];
PK2[1]=myDataTable2.Columns["Model"];
PK2[2]=myDataTable2.Columns["WarehouseId"];
PK2[3]=myDataTable2.Columns["SubWarehouseId"];
PK2[4]=myDataTable2.Columns["Partition"];
myDataTable2.PrimaryKey=PK2;

key2[0]=myDataTable2.Columns["Brand Id"];
key2[1]=myDataTable2.Columns["Model"];
//key2[2]=myDataTable2.Columns["Warehouse"];
//key2[3]=myDataTable2.Columns["Sub Warehouse"];
//key2[4]=myDataTable2.Columns["Partition"];
//myDataTable2.PrimaryKey=key2;

//key2[2]=ds.ProductLocation.QuantityColumn;
//key2=ds.ProductLocation.PrimaryKey;

//myDataTable1.Columns[1].DataType=ds.ProductLocation.ModelColumn.DataType;
DataRelation rel=new DataRelation("myRelation",key1,key2);
myDa
hope that helps
Mohamed Mahfouz
MEA Developer Support Center
ITworx on behalf of Microsoft EMEA GTSC
AddThis Social Bookmark Button