<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<html>
<script language="VB" runat="server">
Sub readDs(Sender As Object, E As EventArgs)
	Dim dv as DataView
	Dim i as integer
	Dim myConnection As OleDbConnection
	Dim myCommand    As OleDbDataAdapter
	Dim ds           As New DataSet
	Dim ConnStr      As String
	Dim SQL          As String
	ConnStr = "Provider=SQLOLEDB; Data Source=(local); Initial Catalog=pubs;User ID=sa;"
	myConnection = New OleDbConnection(ConnStr)    
	'填充 authors 表
	SQL = "select  * from authors "
	myCommand = New OleDbDataAdapter(SQL, myConnection)
	myCommand.Fill(ds, "authors")
	'填充 TitlesAuthor
	SQL = "select  * from titleAuthor"
	myCommand = New OleDbDataAdapter(SQL, myConnection)
	myCommand.Fill(ds, "titleauthor")
	'基于au_id定义一个关系
    'dc1为主表，dc2为从表

	Dim dc1 As DataColumn
	Dim dc2 As DataColumn
	dc1 = ds.Tables("Authors").Columns("au_id")
	dc2 = ds.Tables("titleauthor").Columns("au_id")
	Dim dr As DataRelation
	dr = New DataRelation("vRelation", dc1, dc2)
	ds.Relations.Add(dr)
   	'循环处理关系
	dim child() as datarow
  	dim r as datarow
  	for each r in ds.Tables("Authors").Rows
  	
  	 child = r.GetChildRows(ds.Relations("vRelation"))
  	  for i = 0 to UBound(child)
  	       Response.Write(child(i)("au_id").ToString )
  	   
  	   next
  	 
  	next
end sub

</script>

    <body>

       <h3><font face="Verdana">DataView</font></h3>
        <form  runat=server>
	  <asp:button text="Read Relation" Onclick="readDs" runat=server/>
	</form>
    </body>
</html>

