DataSet.Relations 属性

获取用于将表链接起来并允许从父表浏览到子表的关系的集合。
属性值
包含 DataRelation 对象的集合的 DataRelationCollection;否则为空值(如果不存在任何 DataRelation 对象)。

[Visual Basic]
ExpandedBlockStart.gif ContractedBlock.gif Public   ReadOnly   Property Relations() Property Relations As DataRelationCollection
InBlock.gif

[C#]
ExpandedBlockStart.gif ContractedBlock.gif public  DataRelationCollection Relations  dot.gif {get;}


示例
[Visual Basic] 以下示例通过 Relations 属性打印所有子表的列名。
[Visual Basic]
ExpandedBlockStart.gif ContractedBlock.gif Private   Sub PrintChildRelationRows() Sub PrintChildRelationRows()
InBlock.gif   
' Declare variable to hold the row values.
InBlock.gif
   Dim strRowVals As String
InBlock.gif   
Dim myDataSet As DataSet
InBlock.gif   
' Get the DataSet of a DataGrid that is displaying data of at least two
InBlock.gif
   ' tables.
InBlock.gif
   Dim myTable As DataTable = CType(DataGrid1.DataSource, DataTable)
InBlock.gif   
' Navigate using the Relations.
InBlock.gif
   Dim myRel As DataRelation
InBlock.gif   
Dim row As DataRow
InBlock.gif   
Dim col As DataColumn
InBlock.gif   
' Print the names of each column in each table through the Relations.
InBlock.gif
   For Each myRel In myDataSet.Relations
InBlock.gif      
For Each col in myRel.ChildTable.Columns
InBlock.gif          strRowVals 
&= col.ColumnName & " "
InBlock.gif
      Next
InBlock.gif    
Next
InBlock.gif    
' Display results.
InBlock.gif
    Console.WriteLine(strRowVals)
ExpandedBlockEnd.gif   
End Sub

None.gif

又一个例子:
None.gif              // ADOSample3\form.cs
None.gif
             private   void  button1_Click( object  sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif            
dot.gif {
InBlock.gif                
//create a dataset
InBlock.gif
                DataSet ds=new DataSet("XMLProducts");
InBlock.gif                
//connect to the northwind database and 
InBlock.gif                
//select all of the rows from products table and from suppliers table
InBlock.gif                
//make sure you connect string matches you server configuration
InBlock.gif
                
InBlock.gif                
string source = Login.Connection;
InBlock.gif                
InBlock.gif                SqlConnection conn
=new SqlConnection(source);
InBlock.gif                
InBlock.gif                SqlDataAdapter daProd
=new SqlDataAdapter("SELECT * FROM Products",conn);
InBlock.gif                SqlDataAdapter daSup
=new SqlDataAdapter("SELECT * FROM Suppliers",conn);
InBlock.gif                
//Fill DataSet from both SqlAdapters
InBlock.gif
                daProd.Fill(ds,"products");
InBlock.gif                daSup.Fill(ds,
"suppliers");
InBlock.gif                
//Add the relation
InBlock.gif
                ds.Relations.Add(ds.Tables["suppliers"].Columns["SupplierId"],
InBlock.gif                    ds.Tables[
"products"].Columns["SupplierId"]);
InBlock.gif                
//Write the Xml to a file so we can look at it later
InBlock.gif
                ds.WriteXml("..\\SuppProd.xml",XmlWriteMode.WriteSchema);
InBlock.gif                
//load data into grid
InBlock.gif
                dataGrid1.DataSource=ds;
InBlock.gif                dataGrid1.DataMember
="suppliers";
InBlock.gif                
//create the XmlDataDocument
InBlock.gif
                doc=new XmlDataDocument(ds);
InBlock.gif                
//Select the productname elements and load them in the grid
InBlock.gif
                XmlNodeList nodeLst=doc.SelectNodes("//ProductName");
InBlock.gif     
InBlock.gif                
foreach(XmlNode nd in nodeLst)
InBlock.gif                    listBox1.Items.Add(nd.InnerXml);
InBlock.gif    
ExpandedBlockEnd.gif            }

转载于:https://www.cnblogs.com/myx/archive/2005/01/06/87400.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值