Master/Detail Dataset

S

sippyuconn

Hi

I have marginally played with Master/Detail Datasets that I have created
manually and filled and displayed in Grids
but I have 2 questions now

1)Is there a way thru sql to create a Master/Datail dataset from a DataBase
that has Parent/Child relationships ??

2)Once you have a Master/Detail Dataset is there an easy way to loop thru
the Parent and get all the children records - so as to process 1 record at a
time ???

Thanks
 
J

Ji Zhou \(Wicresoft\)

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
 
C

Colbert Zhou [MSFT]

Hello Sippyuconn,

I am writing to check the status of the issue on your side. Could you
please let me know if the explanation addresses your concern or not? If you
have any questions or concerns, please feel free to let me know. I will be
more than happy to be of assistance.

Have a nice day!

Best regards,
Colbert Zhou ([email protected], remove 'online.')
Microsoft Online Community Support

=================================================
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no rights.
=================================================
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top