Relation between two Dataset

Thursday, 27 October 2011

Relation between two Dataset

    Create relation between two dataset on column name

                connection.Open();
                command = new SqlCommand(firstSql, connection);
                adapter.SelectCommand = command;
                adapter.Fill(ds, "Table1");
                adapter.SelectCommand.CommandText = secondSql;
                adapter.Fill(ds, "Table2");
                adapter.Dispose();
                command.Dispose();
                connection.Close();
                //creating data relations
                DataRelation relation ;
                DataColumn table1Column ;
                DataColumn table2Column ;
                //retrieve column
                table1Column = ds.Tables["Table1"].Columns[0];
                table2Column = ds.Tables["table2"].Columns[0];
                //relating tables
                relation = new DataRelation("relation", table1Column, table2Column);
                //assign relation to dataset
                ds.Relations.Add(relation);

No comments:

Post a Comment