Microsoft Knowledge Base Email Alertz

KBAlertz.com: (309487) - Use this step-by-step guide to copy hierarchical data into a DataSet by using the Fill method of the DataSet class. How to Read Hierarchical Data into a DataSet You can use hierarchical rowsets or chapters (OLE DB type DBTYPE_HCHAPTER and ADO type...

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]

Search KbAlertz

Advanced Search

Webmasters
Put kbAlertz on your website.
[ Click Here for more! ]





ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **


Bug Tracking Software
For bug tracking software or defect tracking software or issue tracking software, visit Axosoft.


Community Site



We Send hundreds of thousands of emails using ASP.NET Email



Expert Web Design & Graphic Design
Design44.com

ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **




Mentioned In








Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks




Article ID: 309487 - Last Review: September 4, 2003 - Revision: 3.0

HOW TO: Read Hierarchical Data into a DataSet in Visual Basic .NET

This article was previously published under Q309487

On This Page

SUMMARY

Use this step-by-step guide to copy hierarchical data into a DataSet by using the Fill method of the DataSet class.

How to Read Hierarchical Data into a DataSet

You can use hierarchical rowsets or chapters (OLE DB type DBTYPE_HCHAPTER and ADO type adChapter) to fill the contents of a DataSet. When the DataAdapter finds a chaptered column during a Fill operation, a DataTable is created for the chaptered column, and that table is filled with the columns and rows from the chapter. The name of the table created for the chaptered column takes its name from both the parent table name and the chaptered column name; it has the following syntax:
ParentTableNameChapteredColumnName
If a table already exists in the DataSet and its name matches the name of the chaptered column, the current table is filled with the chapter data. If there is no column in an existing table that matches a column found in the chapter, a new column is added.

When the Fill operation is completed, the DataSet will contain two tables: Customers and CustomersOrders. The CustomersOrders table represents the chaptered column. An additional column named Orders is added to the Customers table, and an additional column named CustomersOrders is added to the CustomersOrders table. The Orders column in the Customers table is set to auto-increment. A DataRelation, CustomersOrders, is created by using the columns that were added to the tables with Customers as the parent table.

The following sample code uses the Customers and Orders tables that are included in the Microsoft SQL Server Northwind database.
  1. Create a new Visual Basic .NET Console application project.

    Module1.vb is created by default.
  2. If the Code window is not open, right-click Module1.vb in Solution Explorer, and then click View Code.
  3. Delete all of the code from the Code window.
  4. Type or paste the following code in the Code window:
    Imports System.Data.OleDb
    Module Module1
    
        Sub Main()
            Dim nwindConn As OleDbConnection = New OleDbConnection("Provider=MSDataShape;Data Provider=SQLOLEDB;" & _
                                                     "Data Source=localhost;Integrated Security=SSPI;Initial Catalog=northwind")
    
            Dim custDA As OleDbDataAdapter = New OleDbDataAdapter("SHAPE {SELECT CustomerID, CompanyName FROM Customers} " & _
                                                  "  APPEND ({SELECT CustomerID, OrderID FROM Orders} AS Orders " & _
                                                  "  RELATE CustomerID TO CustomerID)", nwindConn)
            Dim custDS As DataSet = New DataSet()
            custDA.Fill(custDS, "Customers")
      Dim CustomerRow, OrderRow As DataRow
            Dim OrderRows As DataRow()
            Dim sline As String
            Dim i As Integer
            CustomerRow = custDS.Tables("Customers").Rows(0)
            Console.WriteLine("Customer Row")
            Console.WriteLine("Customer ID = " & CustomerRow("CustomerId").ToString)
            Console.WriteLine("Orders for Customer ID " & CustomerRow("customerID").ToString)
            ' Retrieve child rows for the order.
            OrderRows = CustomerRow.GetChildRows("CustomersOrders")
            ' Do something with the child rows collection.
            For i = 0 To OrderRows.Length - 1
                OrderRow = OrderRows(i)
                ' Do something with the detail row.
                sline = (OrderRow("OrderId").ToString)
                Console.WriteLine(sline)
            Next
            Console.ReadLine()
        End Sub
    
    End Module
    					
  5. Modify the connection string as appropriate for your environment.
  6. Press F5 to build and run the project. The following output appears in the Command window:
    Customer Row
    Customer ID = ALFKI
    Orders for Customer ID ALFKI
    10643
    10692
    10702
    10835
    10952
    11011
  7. Press ENTER to close the Command window.

REFERENCES

For additional information about a similar topic in Visual Basic 6.0, click the following article number to view the article in the Microsoft Knowledge Base:
189657  (http://kbalertz.com/Feedback.aspx?kbNumber=189657/EN-US/ ) HOWTO: Use the ADO SHAPE Command
For more information about ADO.NET objects and syntax, refer to the following topic in the Microsoft .NET Framework Software Development Kit (SDK) documentation:
Retrieving Data Using DataReader
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpcontheadonetdatareader.asp (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpcontheadonetdatareader.asp)

APPLIES TO
  • Microsoft ADO.NET (included with the .NET Framework)
  • Microsoft ADO.NET 1.1
  • Microsoft Visual Basic .NET 2002 Standard Edition
  • Microsoft Visual Basic .NET 2003 Standard Edition
Keywords: 
kbhowtomaster kbsystemdata KB309487
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate

Be the first to leave feedback, to help others about this knowledge base article.

(Optional) Name

(Optional) Public URL Or Email

Comments
No HTML -- Text Only Please