Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Another Datarelation Question
Message
From
24/07/2003 00:00:50
Keith Payne
Technical Marketing Solutions
Florida, United States
 
 
To
22/07/2003 10:24:32
Alvin Lourdes
Children and Youth Services Cluster
Toronto, Ontario, Canada
General information
Forum:
ASP.NET
Category:
ADO.NET
Miscellaneous
Thread ID:
00812359
Message ID:
00813004
Views:
18
This message has been marked as the solution to the initial question of the thread.
>I am unsure on how to retrieve data from tables 2 tables
>that are related through a relationship table....
>
>This seems to confuse me...!
>
>TableOne
>--------
>TableOneID
>TableOneDesc
>
>TableTwo
>--------
>TableTwoID
>TableTwoDesc
>
>
>TableOne_TableTwo_Relationship
>------------------------------
>TableOne_TableTwo_RelID
>TableOneID
>TableTwoID
>
>I have related TableOne to TableOne_TableTwo_Relationship.
>Then I have related TableTwo to
>TableOne_TableTwo_Relationship.
>
>I am unsure on how to traverse though the set
>relationships to retrieve data. ie) I want to print out
>all TableTwoDesc that have been related to a specific
>TableOneID.
>
>Thanks for your help,
>
>Alvin

Alvin,

I put together some sample code that will show how to get the data. To run the sample, create a Console Application and paste the code into Module1.vb. Then create a DataSet named Test.xsd in the Console Application (Add/New Item.../DataSet) and paste the XML into the file.

Sample code:
Imports System.Data
Imports System.Xml

Module Module1

    Sub Main()

        ' Create a DataSet to hold our test values
        Dim dsTest As New DataSet()
        dsTest.ReadXmlSchema(New XmlTextReader("..\Test.xsd"))

        ' Load the tables with test data
        With dsTest.Tables("TableOne").Rows
            .Add(New Object(1) {1, "Apple"})
            .Add(New Object(1) {2, "Orange"})
            .Add(New Object(1) {3, "Tomato"})
        End With

        With dsTest.Tables("TableTwo").Rows
            .Add(New Object(1) {1, "Fruit"})
            .Add(New Object(1) {2, "Vegetable"})
        End With

        With dsTest.Tables("TableOne_TableTwo_Relationship").Rows
            .Add(New Object(2) {1, 1, 1})
            .Add(New Object(2) {2, 2, 1})
            .Add(New Object(2) {3, 3, 1})
            .Add(New Object(2) {4, 3, 2})
        End With

        ' Since the XML Schema (Test.xsd) already has the Relationships defined,
        ' we are ready to retrieve data from the DataSet

        Dim ItemRow As DataRow
        Dim XRefRow As DataRow
        Dim TypeRow As DataRow

        Console.WriteLine("Displaying all rows from Table One:")
        Console.WriteLine()
        Console.WriteLine("TableOneID | TableOneDesc")
        Console.WriteLine("-------------------------")
        For Each ItemRow In dsTest.Tables("TableOne").Rows
            Console.WriteLine("{0,10}   {1}", ItemRow("TableOneID"), ItemRow("TableOneDesc"))
        Next

        Console.WriteLine()
        Console.WriteLine("Displaying all rows from Table Two:")
        Console.WriteLine()
        Console.WriteLine("TableTwoID | TableTwoDesc")
        Console.WriteLine("-------------------------")
        For Each ItemRow In dsTest.Tables("TableTwo").Rows
            Console.WriteLine("{0,10}   {1}", ItemRow("TableTwoID"), ItemRow("TableTwoDesc"))
        Next

        Console.WriteLine()
        Console.WriteLine("Displaying all rows from Table One and the related rows from Table Two:")
        Console.WriteLine()
        Console.WriteLine("TableOneID | TableOneDesc  +  TableTwoID | TableTwoDesc")
        Console.WriteLine("-------------------------------------------------------")
        For Each ItemRow In dsTest.Tables("TableOne").Rows
            For Each XRefRow In ItemRow.GetChildRows("TableOneTableOne_TableTwo_Relationship")
                TypeRow = dsTest.Tables("TableTwo").Rows.Find(XRefRow("TableTwoID"))
                Console.WriteLine("{0,10}   {1,-12}  +  {2,10}   {3,-12}", _
                ItemRow("TableOneID"), ItemRow("TableOneDesc"), _
                TypeRow("TableTwoID"), TypeRow("TableTwoDesc"))
            Next
        Next
        Console.Write("Press ENTER to close")
        Console.Read()
    End Sub

End Module
Sample XML:
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema id="Test" targetNamespace="http://tempuri.org/Test.xsd" 
	elementFormDefault="qualified" attributeFormDefault="qualified" 
	xmlns="http://tempuri.org/Test.xsd" 
	xmlns:mstns="http://tempuri.org/Test.xsd" 
	xmlns:xs="http://www.w3.org/2001/XMLSchema" 
	xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
	<xs:element name="Test" msdata:IsDataSet="true">
		<xs:complexType>
			<xs:choice maxOccurs="unbounded">
				<xs:element name="TableOne">
					<xs:complexType>
						<xs:sequence>
							<xs:element name="TableOneID" 
							type="xs:integer" />
							<xs:element name="TableOneDesc" 
							type="xs:string" />
						</xs:sequence>
					</xs:complexType>
				</xs:element>
				<xs:element name="TableTwo">
					<xs:complexType>
						<xs:sequence>
							<xs:element name="TableTwoID" 
							type="xs:integer" minOccurs="0" />
							<xs:element name="TableTwoDesc" 
							type="xs:string" minOccurs="0" />
						</xs:sequence>
					</xs:complexType>
				</xs:element>
				<xs:element name="TableOne_TableTwo_Relationship">
					<xs:complexType>
						<xs:sequence>
							<xs:element 
							name="TableOne_TableTwo_RelationshipID" 
							type="xs:integer" minOccurs="0" />
							<xs:element name="TableOneID" 
							type="xs:integer" 
							minOccurs="0" />
							<xs:element name="TableTwoID" 
							type="xs:integer" 
							minOccurs="0" />
						</xs:sequence>
					</xs:complexType>
				</xs:element>
			</xs:choice>
		</xs:complexType>
		<xs:key name="TableOnePrimaryKey" msdata:PrimaryKey="true">
			<xs:selector xpath=".//mstns:TableOne" />
			<xs:field xpath="mstns:TableOneID" />
		</xs:key>
		<xs:key name="TableTwoID" msdata:PrimaryKey="true">
			<xs:selector xpath=".//mstns:TableTwo" />
			<xs:field xpath="mstns:TableTwoID" />
		</xs:key>
		<xs:key name="TableOne_TableTwo_RelationShipPrimaryKey" 
		msdata:PrimaryKey="true">
			<xs:selector 
			xpath=".//mstns:TableOne_TableTwo_Relationship" />
			<xs:field 
			xpath="mstns:TableOne_TableTwo_RelationshipID" />
		</xs:key>
		<xs:keyref name="TableOneTableOne_TableTwo_Relationship" 
		refer="TableOnePrimaryKey">
			<xs:selector 
			xpath=".//mstns:TableOne_TableTwo_Relationship" />
			<xs:field 
			xpath="mstns:TableOneID" />
		</xs:keyref>
		<xs:keyref name="TableTwoTableOne_TableTwo_Relationship" 
		refer="TableTwoID">
			<xs:selector 
			xpath=".//mstns:TableOne_TableTwo_Relationship" />
			<xs:field 
			xpath="mstns:TableTwoID" />
		</xs:keyref>
	</xs:element>
</xs:schema>
Good luck!
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform