Export (0) Print
Expand All

DataSet.Load Method (IDataReader, LoadOption, DataTable[])

Note: This method is new in the .NET Framework version 2.0.

Fills a DataSet with values from a data source using the supplied IDataReader, using an array of DataTable instances to supply the schema and namespace information.

Namespace: System.Data
Assembly: System.Data (in system.data.dll)

'Declaration
Public Sub Load ( _
	reader As IDataReader, _
	loadOption As LoadOption, _
	ParamArray tables As DataTable() _
)
'Usage
Dim instance As DataSet
Dim reader As IDataReader
Dim loadOption As LoadOption
Dim tables As DataTable()

instance.Load(reader, loadOption, tables)
public void Load (
	IDataReader reader, 
	LoadOption loadOption, 
	DataTable[] tables
)
public function Load (
	reader : IDataReader, 
	loadOption : LoadOption, 
	... tables : DataTable[]
)

Parameters

reader

An IDataReader that provides one or more result sets.

loadOption

A value from the LoadOption enumeration that indicates how rows already in the DataTable instances within the DataSet will be combined with incoming rows that share the same primary key.

tables

An array of DataTable instances, from which the Load method retrieves name and namespace information. Each of these tables must be a member of the DataTableCollection contained by this DataSet.

The Load method provides a technique for filling a single DataTable with data, retrieved from an IDataReader instance. This method provides the same functionality, but allows you to load multiple result sets from an IDataReader into multiple tables within a DataSet.

The loadOption parameter allows you to specify how you want the imported data to interact with existing data, and can be any of the values from the LoadOption enumeration. See the documentation for the DataTableLoad method for more information on using this parameter.

The tables parameter allows you to specify an array of DataTable instances, indicating the order of the tables corresponding to each result set loaded from the reader. The Loadmethod fills each supplied DataTable instance with data from a single result set from the source data reader. After each result set, the Loadmethod moves on to the next result set within the reader, until there are no more result sets.

The name resolution scheme for this method is the same as that followed by the Fill method of the DbDataAdapter class.

The following example creates a new DataSet, adds two DataTable instances to the DataSet, and then fills the DataSet using the Load method, retrieving data from a DataTableReader that contains two result sets. Finally, the example displays the contents of the tables in the console window.

Sub Main()
    Dim dataSet As New DataSet

    Dim customerTable As New DataTable
    Dim productTable As New DataTable

    ' This information is cosmetic, only.
    customerTable.TableName = "Customers"
    productTable.TableName = "Products"

    ' Add the tables to the DataSet:
    dataSet.Tables.Add(customerTable)
    dataSet.Tables.Add(productTable)

    ' Load the data into the existing DataSet. 
    Dim reader As DataTableReader = GetReader()
    dataSet.Load(reader, LoadOption.OverwriteChanges, _
        customerTable, productTable)

    ' Print out the contents of each table:
    For Each table As DataTable In dataSet.Tables
        PrintColumns(table)
    Next

    Console.WriteLine("Press any key to continue.")
    Console.ReadKey()
End Sub

Private Function GetCustomers() As DataTable
    ' Create sample Customers table.
    Dim table As New DataTable
    table.TableName = "Customers"

    ' Create two columns, ID and Name.
    Dim idColumn As DataColumn = table.Columns.Add("ID", _
        GetType(Integer))
    table.Columns.Add("Name", GetType(String))

    ' Set the ID column as the primary key column.
    table.PrimaryKey = New DataColumn() {idColumn

    table.Rows.Add(New Object() {0, "Mary")
    table.Rows.Add(New Object() {1, "Andy")
    table.Rows.Add(New Object() {2, "Peter")
    table.AcceptChanges()
    Return table
End Function

Private Function GetProducts() As DataTable
    ' Create sample Products table, in order
    ' to demonstrate the behavior of the DataTableReader.
    Dim table As New DataTable
    table.TableName = "Products"

    ' Create two columns, ID and Name.
    Dim idColumn As DataColumn = table.Columns.Add("ID", _
        GetType(Integer))
    table.Columns.Add("Name", GetType(String))

    ' Set the ID column as the primary key column.
    table.PrimaryKey = New DataColumn() {idColumn

    table.Rows.Add(New Object() {0, "Wireless Network Card")
    table.Rows.Add(New Object() {1, "Hard Drive")
    table.Rows.Add(New Object() {2, "Monitor")
    table.Rows.Add(New Object() {3, "CPU")
    Return table
End Function

Private Function GetReader() As DataTableReader
    ' Return a DataTableReader containing multiple
    ' result sets, just for the sake of this demo.
    Dim dataSet As New DataSet
    dataSet.Tables.Add(GetCustomers())
    dataSet.Tables.Add(GetProducts())
    Return dataSet.CreateDataReader()
End Function

Private Sub PrintColumns( _
   ByVal table As DataTable)

    Console.WriteLine()
    Console.WriteLine(table.TableName)
    Console.WriteLine("=========================")
    ' Loop through all the rows in the table.
    For Each row As DataRow In table.Rows
        For Each col As DataColumn In table.Columns
            Console.Write(row(col).ToString() & " ")
        Next
        Console.WriteLine()
    Next
End Sub

The example displays the following output in the Console window:

Customers
=========================
0 Mary
1 Andy
2 Peter

Products
=========================
0 Wireless Network Card
1 Hard Drive
2 Monitor
3 CPU

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0

.NET Compact Framework

Supported in: 2.0

Community Additions

ADD
Show:
© 2014 Microsoft