Microsoft KB Archive/316260

From BetaArchive Wiki
Knowledge Base


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

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

  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"), _
            myDS.Tables("Orders").Columns("CustomerID"))
    
            myDr = myCustomers.NewRow()
            myDr("CustomerID") = "9876"
            myDr("CompanyName") = "Lucerne Publishing"
            myDr("ContactName") = "Kim Ralls"       
    
            myCustomers.Rows.Add(myDr)
    
            myDr = myOrders.NewRow()
            myDr("OrderID") = 6521
            myDr("CustomerID") = "9876"
            myDr("EmployeeID") = 852
            myDr("OrderDate") = #1/5/2002#
            myDr("RequiredDate") = #2/1/2002#
            myOrders.Rows.Add(myDr)
    
            Console.WriteLine(myDS.GetXml())
                        
  4. Press CTRL+F5 to run the application and observe the output.

back to the top

REFERENCES

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


back to the top


Additional query words: relational

Keywords: kbhowtomaster kbsystemdata KB316260