Microsoft KB Archive/316260

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

Article ID: 316260

Article Last Modified on 8/14/2006

-

APPLIES TO


 * Microsoft ADO.NET 1.1
 * Microsoft ADO.NET 1.0
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition

-



This article was previously published under Q316260



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

IN THIS TASK
SUMMARY
 * Requirements
 * Create the Project

REFERENCES



SUMMARY
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.

back to the top

Requirements
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

back to the top

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

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

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

myDS.Relations.Add(&quot;rel_Customers_Orders&quot;, _       myDS.Tables(&quot;Customers&quot;).Columns(&quot;CustomerID&quot;), _        myDS.Tables(&quot;Orders&quot;).Columns(&quot;CustomerID&quot;))

myDr = myCustomers.NewRow myDr(&quot;CustomerID&quot;) = &quot;9876&quot; myDr(&quot;CompanyName&quot;) = &quot;Lucerne Publishing&quot; myDr(&quot;ContactName&quot;) = &quot;Kim Ralls&quot;

myCustomers.Rows.Add(myDr)

myDr = myOrders.NewRow myDr(&quot;OrderID&quot;) = 6521 myDr(&quot;CustomerID&quot;) = &quot;9876&quot; myDr(&quot;EmployeeID&quot;) = 852 myDr(&quot;OrderDate&quot;) = #1/5/2002# myDr(&quot;RequiredDate&quot;) = #2/1/2002# myOrders.Rows.Add(myDr)

Console.WriteLine(myDS.GetXml)  Press CTRL+F5 to run the application and observe the output.

back to the top

