HOW TO: Programmatically Create a Hierarchical DataSet Object with ADO.NET in Visual Basic .NET

This article refers to the following Microsoft .NET Framework Class Library namespaces:
  • System.Data
  • System.XML



This step-by-step article describes how to create a hierarchical DataSet object that you can use as a structure for your programs. You may not have the exact data or structure that you want to use for your project. You can use the method in this article to create prototypes for your examples. It may also be easier to submit only the data that you do have to the database without using the shape command syntax to send the data to the database. You might also have a limited set of data that you must add to the database. Instead of passing 20 arguments about the data to a method, you can pass the programmatically created DataSet object. This article describes how to use this method.

This example in this article creates a new customer and creates related order information, and outputs the data as XML. You start by defining the DataSet object, and then create two tables and a row. Then, you define the columns, add a relationship between the tables, fill the rows with data, append rows to the rows collection, and present the data in XML format.


The following list outlines the recommended hardware, software, network infrastructure, and service packs that are required:
  • Microsoft Visual Studio .NET installed on a compatible Microsoft Windows operating system
This article assumes that you are familiar with the following topics:
  • Visual Basic .NET
  • ADO.NET data access

Create the Project

  1. Start Visual Studio .NET, and then create a new Visual Basic .NET Console application.
  2. Add the following IMPORTS statements to the top of the code window:
    Imports System
    Imports System.Data
    Imports System.XML
  3. In the Sub Main section, add the following code:
    Dim myDS As New Data.DataSet("CusOrd")
    Dim myCustomers As Data.DataTable = myDS.Tables.Add("Customers")
    Dim myOrders As Data.DataTable = myDS.Tables.Add("Orders")
    Dim myDr As Data.DataRow

    With myCustomers
    .Columns.Add("CustomerID", Type.GetType("System.String"))
    .Columns.Add("CompanyName", Type.GetType("System.String"))
    .Columns.Add("ContactName", Type.GetType("System.String"))
    End With

    With myOrders
    .Columns.Add("OrderID", Type.GetType("System.Int32"))
    .Columns.Add("CustomerID", Type.GetType("System.String"))
    .Columns.Add("EmployeeID", Type.GetType("System.Int32"))
    .Columns.Add("OrderDate", Type.GetType("System.DateTime"))
    .Columns.Add("RequiredDate", Type.GetType("System.DateTime"))
    End With

    myDS.Relations.Add("rel_Customers_Orders", _
    myDS.Tables("Customers").Columns("CustomerID"), _

    myDr = myCustomers.NewRow()
    myDr("CustomerID") = "9876"
    myDr("CompanyName") = "Lucerne Publishing"
    myDr("ContactName") = "Kim Ralls"


    myDr = myOrders.NewRow()
    myDr("OrderID") = 6521
    myDr("CustomerID") = "9876"
    myDr("EmployeeID") = 852
    myDr("OrderDate") = #1/5/2002#
    myDr("RequiredDate") = #2/1/2002#

  4. Press CTRL+F5 to run the application and observe the output.


For additional information about how to populate a DataSet object in Visual Basic .NET, click the article number below to view the article in the Microsoft Knowledge Base:
301216 HOW TO: Populate a DataSet Object from a Database by Using Visual Basic .NET

ID do Artigo: 316260 - Última Revisão: 23 de mar de 2009 - Revisão: 1