Hello Sippyuconn,
Thanks for your posting in the Microsoft Newsgroup and Wish you have a happy
new year!
Based on my understanding, you have some concerns related to the following
two topics,
1.Create a Master/Detail dataset from a database through sql.
2.Once we have a Master/Detail dataset, how to loop through the parent and
get all the children records.
If I have misunderstood your questions, please feel free to correct so that
I can provide a more accurate support on this!
As far as I know, the Master/Detail terms are usually used to modify the
Forms or GridView controls in form, rather than the DataSet. It means that
when we choose a row in datagrid1, datagrid2 will be filled with the
childrows of the datagrid1's selected item, based on a relation ship.
Surely, we have some request upon the source dataset that it must include a
data relation. Are you reffering this kind of dataset as "Master/Detail
dataset"?
If that is the case, I think we can use the following codes to create the
"Master/Detail dataset",
try
{
String connectionString =
"Integrated Security=SSPI;Persist Security Info=False;" +
"Initial Catalog=Northwind;Data Source=localhost";
SqlConnection connection = new SqlConnection(connectionString);
// Create a DataSet.
DataSet masterDetailDataSet = new DataSet();
masterDetailDataSet.Locale =
System.Globalization.CultureInfo.InvariantCulture;
// Add data from the Customers table to the DataSet.
SqlDataAdapter masterDataAdapter = new
SqlDataAdapter("select * from Customers", connection);
masterDataAdapter.Fill(masterDetailDataSet, "Customers");
// Add data from the Orders table to the DataSet.
SqlDataAdapter detailsDataAdapter = new
SqlDataAdapter("select * from Orders", connection);
detailsDataAdapter.Fill(masterDetailDataSet, "Orders");
// Establish a relationship between the two tables.
DataRelation relation = new DataRelation("CustomersOrders",
masterDetailDataSet.Tables["Customers"].Columns["CustomerID"],
masterDetailDataSet.Tables["Orders"].Columns["CustomerID"]);
masterDetailDataSet.Relations.Add(relation);
// Bind the master data connector to the Customers table.
masterBindingSource.DataSource = masterDetailDataSet;
masterBindingSource.DataMember = "Customers";
// Bind the details data connector to the master data connector,
// using the DataRelation name to filter the information in the
// details table based on the current row in the master table.
detailsBindingSource.DataSource = masterBindingSource;
detailsBindingSource.DataMember = "CustomersOrders";
}
catch (SqlException)
{
MessageBox.Show("To run this example, replace the value of the " +
"connectionString variable with a connection string that is " +
"valid for your system.");
}
More detailed information, please refer this link
http://msdn.microsoft.com/en-us/library/y8c0cxey.aspx.
About how to loop through the parent records and get all the children
records, please use the below codes which work fine in my side,
foreach (DataRow row in masterDetailDataSet.Tables["Customers"].Rows)
{
Debug.Print("The customer " + row["CustomerID"].ToString() + " has the
following orders");
foreach (DataRow childRow in
row.GetChildRows(masterDetailDataSet.Relations["CustomersOrders"]))
{
Debug.Print(" " + childRow["OrderID"].ToString());
}
}
We can access the child rows by calling a row's GetChildRows method. See
http://msdn.microsoft.com/en-us/library/system.data.datarow.getchildrows.aspx.
Please let me know if my explanation addresses your concerns? If you need
future assistance on this, do not hesitate to contact me.
Good day!
Best regards,
Ji Zhou
Microsoft Newsgroup Online Support