PrivateSub PrintChildRelationRows()Sub PrintChildRelationRows()
' Declare variable to hold the row values. Dim strRowVals AsString Dim myDataSet As DataSet
' Get the DataSet of a DataGrid that is displaying data of at least two ' tables. Dim myTable As DataTable =CType(DataGrid1.DataSource, DataTable)
' Navigate using the Relations. Dim myRel As DataRelation
Dim row As DataRow
Dim col As DataColumn
' Print the names of each column in each table through the Relations. ForEach myRel In myDataSet.Relations
ForEach col in myRel.ChildTable.Columns
strRowVals &= col.ColumnName &" " Next Next ' Display results. Console.WriteLine(strRowVals)
End Sub
又一个例子:
//ADOSample3\form.cs privatevoid button1_Click(object sender, System.EventArgs e)
{
//create a dataset DataSet ds=new DataSet("XMLProducts");
//connect to the northwind database and
//select all of the rows from products table and from suppliers table
//make sure you connect string matches you server configuration string source = Login.Connection;
SqlConnection conn=new SqlConnection(source);
SqlDataAdapter daProd=new SqlDataAdapter("SELECT * FROM Products",conn);
SqlDataAdapter daSup=new SqlDataAdapter("SELECT * FROM Suppliers",conn);
//Fill DataSet from both SqlAdapters daProd.Fill(ds,"products");
daSup.Fill(ds,"suppliers");
//Add the relation ds.Relations.Add(ds.Tables["suppliers"].Columns["SupplierId"],
ds.Tables["products"].Columns["SupplierId"]);
//Write the Xml to a file so we can look at it later ds.WriteXml("..\\SuppProd.xml",XmlWriteMode.WriteSchema);
//load data into grid dataGrid1.DataSource=ds;
dataGrid1.DataMember="suppliers";
//create the XmlDataDocument doc=new XmlDataDocument(ds);
//Select the productname elements and load them in the grid XmlNodeList nodeLst=doc.SelectNodes("//ProductName");
foreach(XmlNode nd in nodeLst)
listBox1.Items.Add(nd.InnerXml);
}