属性值
包含 DataRelation 对象的集合的 DataRelationCollection;否则为空值(如果不存在任何 DataRelation 对象)。
[Visual Basic]
Public ReadOnly Property Relations As DataRelationCollection
[C#]
public DataRelationCollection Relations {get;}
示例
[Visual Basic] 以下示例通过 Relations 属性打印所有子表的列名。
[Visual Basic]
Private Sub PrintChildRelationRows()
' Declare variable to hold the row values.
Dim strRowVals As String
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.
For Each myRel In myDataSet.Relations
For Each col in myRel.ChildTable.Columns
strRowVals &= col.ColumnName & " "
Next
Next
' Display results.
Console.WriteLine(strRowVals)
End Sub
' Declare variable to hold the row values.
Dim strRowVals As String
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.
For Each myRel In myDataSet.Relations
For Each col in myRel.ChildTable.Columns
strRowVals &= col.ColumnName & " "
Next
Next
' Display results.
Console.WriteLine(strRowVals)
End Sub
又一个例子:
//ADOSample3\form.cs
private void 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);
}
private void 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);
}