本文整理匯總了VB.NET中System.Data.DataSet.Load方法的典型用法代碼示例。如果您正苦於以下問題:VB.NET DataSet.Load方法的具體用法?VB.NET DataSet.Load怎麽用?VB.NET DataSet.Load使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.Data.DataSet
的用法示例。
在下文中一共展示了DataSet.Load方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的VB.NET代碼示例。
示例1: Main
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
示例2: Main
Sub Main()
Dim dataSet As New DataSet
Dim table As DataTable
Dim reader As DataTableReader = GetReader()
' The tables listed as parameters for the Load method
' should be in the same order as the tables within the IDataReader.
dataSet.Load(reader, LoadOption.Upsert, "Customers", "Products")
For Each table In dataSet.Tables
PrintColumns(table)
Next
' Now try the example with the DataSet
' already filled with data:
dataSet = New DataSet
dataSet.Tables.Add(GetCustomers())
dataSet.Tables.Add(GetProducts())
' Retrieve a data reader containing changed data:
reader = GetReader()
' Load the data into the existing DataSet. Retrieve the order of the
' the data in the reader from the
' list of table names in the parameters. If you specify
' a new table name here, the Load method will create
' a corresponding new table.
dataSet.Load(reader, LoadOption.Upsert, "NewCustomers", "Products")
For Each table 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
示例3: Main
Sub Main()
Dim dataSet As New DataSet
Dim table As New DataTable()
' Attempt to load data from a data reader in which
' the schema is incompatible with the current schema.
' If you use exception handling, you won't get the chance
' to examine each row, and each individual table,
' as the Load method progresses.
' By taking advantage of the FillErrorEventHandler delegate,
' you can interact with the Load process as an error occurs,
' attempting to fix the problem, or simply continuing or quitting
' the Load process.:
dataSet = New DataSet()
table = GetIntegerTable()
dataSet.Tables.Add(table)
Dim reader As New DataTableReader(GetStringTable())
dataSet.Load(reader, LoadOption.OverwriteChanges, _
AddressOf FillErrorHandler, table)
Console.WriteLine("Press any key to continue.")
Console.ReadKey()
End Sub
Private Sub FillErrorHandler(ByVal sender As Object, _
ByVal e As FillErrorEventArgs)
' You can use the e.Errors value to determine exactly what
' went wrong.
If e.Errors.GetType Is GetType(System.FormatException) Then
Console.WriteLine("Error when attempting to update the value: {0}", _
e.Values(0))
End If
' Setting e.Continue to True tells the Load
' method to continue trying. Setting it to False
' indicates that an error has occurred, and the
' Load method raises the exception that got
' you here.
e.Continue = True
End Sub
Private Function GetIntegerTable() As DataTable
' Create sample table with a single Int32 column.
Dim table As New DataTable
Dim idColumn As DataColumn = table.Columns.Add("ID", _
GetType(Integer))
' Set the ID column as the primary key column.
table.PrimaryKey = New DataColumn() {idColumn}
table.Rows.Add(New Object() {4})
table.Rows.Add(New Object() {5})
table.TableName = "IntegerTable"
table.AcceptChanges()
Return table
End Function
Private Function GetStringTable() As DataTable
' Create sample table with a single String column.
Dim table As New DataTable
Dim idColumn As DataColumn = table.Columns.Add("ID", _
GetType(String))
' Set the ID column as the primary key column.
table.PrimaryKey = New DataColumn() {idColumn}
table.Rows.Add(New Object() {"Mary"})
table.Rows.Add(New Object() {"Andy"})
table.Rows.Add(New Object() {"Peter"})
table.AcceptChanges()
Return table
End Function
Private Sub PrintColumns( _
ByVal table As DataTable)
' Loop through all the rows in the DataTableReader.
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