本文整理汇总了VB.NET中System.Data.DataTable.Load方法的典型用法代码示例。如果您正苦于以下问题:VB.NET DataTable.Load方法的具体用法?VB.NET DataTable.Load怎么用?VB.NET DataTable.Load使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Data.DataTable 的用法示例。
在下文中一共展示了DataTable.Load方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的VB.NET代码示例。
示例1: Main
Sub Main()
Dim table As New DataTable()
' This example examines a number of scenarios involving the
' DataTable.Load method.
Console.WriteLine("Load a DataTable and infer its schema:")
' Retrieve a data reader, based on the Customers data. In
' an application, this data might be coming from a middle-tier
' business object:
Dim reader As New DataTableReader(GetCustomers())
' The table has no schema. The Load method will infer the
' schema from the IDataReader:
table.Load(reader)
PrintColumns(table)
Console.WriteLine(" ============================= ")
Console.WriteLine("Load a DataTable from an incompatible IDataReader:")
' Create a table with a single integer column. Attempt
' to load data from a reader with a schema that is
' incompatible. Note the exception, determined
' by the particular incompatibility:
table = GetIntegerTable()
reader = New DataTableReader(GetStringTable())
Try
table.Load(reader)
Catch ex As Exception
Console.WriteLine(ex.GetType.Name & ":" & ex.Message())
End Try
Console.WriteLine(" ============================= ")
Console.WriteLine( _
"Load a DataTable with an IDataReader that has extra columns:")
' Note that loading a reader with extra columns adds
' the columns to the existing table, if possible:
table = GetIntegerTable()
reader = New DataTableReader(GetCustomers())
table.Load(reader)
PrintColumns(table)
Console.WriteLine(" ============================= ")
Console.WriteLine( _
"Load a DataTable with an IDataReader that has missing columns:")
' Note that loading a reader with missing columns causes
' the columns to be filled with null data, if possible:
table = GetCustomers()
reader = New DataTableReader(GetIntegerTable())
table.Load(reader)
PrintColumns(table)
' Demonstrate the various possibilites when loading data into
' a DataTable that already contains data.
Console.WriteLine(" ============================= ")
Console.WriteLine("Demonstrate data considerations:")
Console.WriteLine("Current value, Original value, (RowState)")
Console.WriteLine(" ============================= ")
Console.WriteLine("Original table:")
table = SetupModifiedRows()
DisplayRowState(table)
Console.WriteLine(" ============================= ")
Console.WriteLine("Data in IDataReader to be loaded:")
DisplayRowState(GetChangedCustomers())
PerformDemo(LoadOption.OverwriteChanges)
PerformDemo(LoadOption.PreserveChanges)
PerformDemo(LoadOption.Upsert)
Console.WriteLine("Press any key to continue.")
Console.ReadKey()
End Sub
Private Sub DisplayRowState(ByVal table As DataTable)
For i As Integer = 0 To table.Rows.Count - 1
Dim current As Object = "--"
Dim original As Object = "--"
Dim rowState As DataRowState = table.Rows(i).RowState
' Attempt to retrieve the current value, which doesn't exist
' for deleted rows:
If rowState <> DataRowState.Deleted Then
current = table.Rows(i)("Name", DataRowVersion.Current)
End If
' Attempt to retrieve the original value, which doesn't exist
' for added rows:
If rowState <> DataRowState.Added Then
original = table.Rows(i)("Name", DataRowVersion.Original)
End If
Console.WriteLine("{0}: {1}, {2} ({3})", i, current, original, rowState)
Next
End Sub
Private Function GetChangedCustomers() As DataTable
' Create sample Customers table.
Dim table As New DataTable
' 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, "XXX"})
table.Rows.Add(New Object() {1, "XXX"})
table.Rows.Add(New Object() {2, "XXX"})
table.Rows.Add(New Object() {3, "XXX"})
table.Rows.Add(New Object() {4, "XXX"})
table.AcceptChanges()
Return table
End Function
Private Function GetCustomers() As DataTable
' Create sample Customers table.
Dim table As New DataTable
' 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 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.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 PerformDemo(ByVal optionForLoad As LoadOption)
' Load data into a DataTable, retrieve a DataTableReader containing
' different data, and call the Load method. Depending on the
' LoadOption value passed as a parameter, this procedure displays
' different results in the DataTable.
Console.WriteLine(" ============================= ")
Console.WriteLine("table.Load(reader, {0})", optionForLoad)
Console.WriteLine(" ============================= ")
Dim table As DataTable = SetupModifiedRows()
Dim reader As New DataTableReader(GetChangedCustomers())
AddHandler table.RowChanging, New _
DataRowChangeEventHandler(AddressOf HandleRowChanging)
table.Load(reader, optionForLoad)
Console.WriteLine()
DisplayRowState(table)
End Sub
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
Private Function SetupModifiedRows() As DataTable
' Fill a DataTable with customer info, and
' then modify, delete, and add rows.
Dim table As DataTable = GetCustomers()
' Row 0 is unmodified.
' Row 1 is modified.
' Row 2 is deleted.
' Row 3 is added.
table.Rows(1)("Name") = "Sydney"
table.Rows(2).Delete()
Dim row As DataRow = table.NewRow
row("ID") = 3
row("Name") = "Melony"
table.Rows.Add(row)
' Note that the code doesn't call
' table.AcceptChanges()
Return table
End Function
Private Sub HandleRowChanging(ByVal sender As Object, _
ByVal e As System.Data.DataRowChangeEventArgs)
Console.WriteLine( _
"RowChanging event: ID = {0}, action = {1}", e.Row("ID"), _
e.Action)
End Sub
开发者ID:VB.NET开发者,项目名称:System.Data,代码行数:226,代码来源:DataTable.Load
示例2: Main
Sub Main()
' This example examines a number of scenarios involving the
' DataTable.Load method.
Console.WriteLine("Load a DataTable and infer its schema:")
' The table has no schema. The Load method will infer the
' schema from the IDataReader:
Dim table As New DataTable()
' Retrieve a data reader, based on the Customers data. In
' an application, this data might be coming from a middle-tier
' business object:
Dim reader As New DataTableReader(GetCustomers())
table.Load(reader)
PrintColumns(table)
Console.WriteLine(" ============================= ")
Console.WriteLine( _
"Load a DataTable from an incompatible IDataReader:")
' Create a table with a single integer column. Attempt
' to load data from a reader with a schema that is
' incompatible. Note the exception, determined
' by the particular incompatibility:
table = GetIntegerTable()
reader = New DataTableReader(GetStringTable())
Try
table.Load(reader)
Catch ex As Exception
Console.WriteLine(ex.GetType.Name & ":" & ex.Message())
End Try
Console.WriteLine(" ============================= ")
Console.WriteLine( _
"Load a DataTable with an IDataReader that has extra columns:")
' Note that loading a reader with extra columns adds
' the columns to the existing table, if possible:
table = GetIntegerTable()
reader = New DataTableReader(GetCustomers())
table.Load(reader)
PrintColumns(table)
Console.WriteLine(" ============================= ")
Console.WriteLine( _
"Load a DataTable with an IDataReader that has missing columns:")
' Note that loading a reader with missing columns causes
' the columns to be filled with null data, if possible:
table = GetCustomers()
reader = New DataTableReader(GetIntegerTable())
table.Load(reader)
PrintColumns(table)
' Demonstrate the various possibilites when loading data into
' a DataTable that already contains data.
Console.WriteLine(" ============================= ")
Console.WriteLine("Demonstrate data considerations:")
Console.WriteLine("Current value, Original value, (RowState)")
Console.WriteLine(" ============================= ")
Console.WriteLine("Original table:")
table = SetupModifiedRows()
DisplayRowState(table)
Console.WriteLine(" ============================= ")
Console.WriteLine("Data in IDataReader to be loaded:")
DisplayRowState(GetChangedCustomers())
' Load data into a DataTable, retrieve a DataTableReader
' containing different data, and call the Load method.
Console.WriteLine(" ============================= ")
Console.WriteLine("table.Load(reader)")
Console.WriteLine(" ============================= ")
table = SetupModifiedRows()
reader = New DataTableReader(GetChangedCustomers())
table.Load(reader)
DisplayRowState(table)
Console.WriteLine("Press any key to continue.")
Console.ReadKey()
End Sub
Private Sub DisplayRowState(ByVal table As DataTable)
For i As Integer = 0 To table.Rows.Count - 1
Dim current As Object = "--"
Dim original As Object = "--"
Dim rowState As DataRowState = table.Rows(i).RowState
' Attempt to retrieve the current value, which doesn't exist
' for deleted rows:
If rowState <> DataRowState.Deleted Then
current = table.Rows(i)("Name", DataRowVersion.Current)
End If
' Attempt to retrieve the original value, which doesn't exist
' for added rows:
If rowState <> DataRowState.Added Then
original = table.Rows(i)("Name", DataRowVersion.Original)
End If
Console.WriteLine("{0}: {1}, {2} ({3})", i, _
current, original, rowState)
Next
End Sub
Private Function GetChangedCustomers() As DataTable
' Create sample Customers table.
Dim table As New DataTable
' 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() {1, "XXX"})
table.Rows.Add(New Object() {2, "XXX"})
table.Rows.Add(New Object() {3, "XXX"})
table.Rows.Add(New Object() {4, "XXX"})
table.Rows.Add(New Object() {5, "XXX"})
table.Rows.Add(New Object() {6, "XXX"})
table.AcceptChanges()
Return table
End Function
Private Function GetCustomers() As DataTable
' Create sample Customers table.
Dim table As New DataTable
' 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() {1, "Mary"})
table.Rows.Add(New Object() {2, "Andy"})
table.Rows.Add(New Object() {3, "Peter"})
table.Rows.Add(New Object() {4, "Russ"})
table.AcceptChanges()
Return table
End Function
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() {5})
table.Rows.Add(New Object() {6})
table.Rows.Add(New Object() {7})
table.Rows.Add(New Object() {8})
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.Rows.Add(New Object() {"Russ"})
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
Private Function SetupModifiedRows() As DataTable
' Fill a DataTable with customer info, and
' then modify, delete, and add rows.
Dim table As DataTable = GetCustomers()
' Row 0 is unmodified.
' Row 1 is modified.
' Row 2 is deleted.
' Row 5 is added.
table.Rows(1)("Name") = "Sydney"
table.Rows(2).Delete()
Dim row As DataRow = table.NewRow
row("ID") = 5
row("Name") = "Melony"
table.Rows.Add(row)
' Note that the code doesn't call
' table.AcceptChanges()
Return table
End Function
开发者ID:VB.NET开发者,项目名称:System.Data,代码行数:217,代码来源:DataTable.Load
示例3: Main
Sub Main()
Dim table As New DataTable()
' This example examines a number of scenarios involving the
' DataTable.Load method.
Console.WriteLine("Load a DataTable and infer its schema:")
' Retrieve a data reader, based on the Customers data. In
' an application, this data might be coming from a middle-tier
' business object:
Dim reader As New DataTableReader(GetCustomers())
' The table has no schema. The Load method will infer the
' schema from the IDataReader:
table.Load(reader)
PrintColumns(table)
Console.WriteLine(" ============================= ")
Console.WriteLine( _
"Load a DataTable from an incompatible IDataReader:")
' Create a table with a single integer column. Attempt
' to load data from a reader with a schema that is
' incompatible. Note the exception, determined
' by the particular incompatibility:
table = GetIntegerTable()
reader = New DataTableReader(GetStringTable())
Try
table.Load(reader)
Catch ex As Exception
Console.WriteLine(ex.GetType.Name & ":" & ex.Message())
End Try
Console.WriteLine(" ============================= ")
Console.WriteLine( _
"Load a DataTable with an IDataReader that has extra columns:")
' Note that loading a reader with extra columns adds
' the columns to the existing table, if possible:
table = GetIntegerTable()
reader = New DataTableReader(GetCustomers())
table.Load(reader)
PrintColumns(table)
Console.WriteLine(" ============================= ")
Console.WriteLine( _
"Load a DataTable with an IDataReader that has missing columns:")
' Note that loading a reader with missing columns causes
' the columns to be filled with null data, if possible:
table = GetCustomers()
reader = New DataTableReader(GetIntegerTable())
table.Load(reader)
PrintColumns(table)
' Demonstrate the various possibilites when loading data into
' a DataTable that already contains data.
Console.WriteLine(" ============================= ")
Console.WriteLine("Demonstrate data considerations:")
Console.WriteLine("Current value, Original value, (RowState)")
Console.WriteLine(" ============================= ")
Console.WriteLine("Original table:")
table = SetupModifiedRows()
DisplayRowState(table)
Console.WriteLine(" ============================= ")
Console.WriteLine("Data in IDataReader to be loaded:")
DisplayRowState(GetChangedCustomers())
PerformDemo(LoadOption.OverwriteChanges)
PerformDemo(LoadOption.PreserveChanges)
PerformDemo(LoadOption.Upsert)
Console.WriteLine("Press any key to continue.")
Console.ReadKey()
End Sub
Private Sub DisplayRowState(ByVal table As DataTable)
For i As Integer = 0 To table.Rows.Count - 1
Dim current As Object = "--"
Dim original As Object = "--"
Dim rowState As DataRowState = table.Rows(i).RowState
' Attempt to retrieve the current value, which doesn't exist
' for deleted rows:
If rowState <> DataRowState.Deleted Then
current = table.Rows(i)("Name", DataRowVersion.Current)
End If
' Attempt to retrieve the original value, which doesn't exist
' for added rows:
If rowState <> DataRowState.Added Then
original = table.Rows(i)("Name", DataRowVersion.Original)
End If
Console.WriteLine("{0}: {1}, {2} ({3})", i, _
current, original, rowState)
Next
End Sub
Private Function GetChangedCustomers() As DataTable
' Create sample Customers table.
Dim table As New DataTable
' 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, "XXX"})
table.Rows.Add(New Object() {1, "XXX"})
table.Rows.Add(New Object() {2, "XXX"})
table.Rows.Add(New Object() {3, "XXX"})
table.Rows.Add(New Object() {4, "XXX"})
table.AcceptChanges()
Return table
End Function
Private Function GetCustomers() As DataTable
' Create sample Customers table.
Dim table As New DataTable
' 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 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.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 PerformDemo(ByVal optionForLoad As LoadOption)
' Load data into a DataTable, retrieve a DataTableReader containing
' different data, and call the Load method. Depending on the
' LoadOption value passed as a parameter, this procedure displays
' different results in the DataTable.
Console.WriteLine(" ============================= ")
Console.WriteLine("table.Load(reader, {0})", optionForLoad)
Console.WriteLine(" ============================= ")
Dim table As DataTable = SetupModifiedRows()
Dim reader As New DataTableReader(GetChangedCustomers())
AddHandler table.RowChanging, New _
DataRowChangeEventHandler(AddressOf HandleRowChanging)
table.Load(reader, optionForLoad)
Console.WriteLine()
DisplayRowState(table)
End Sub
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
Private Function SetupModifiedRows() As DataTable
' Fill a DataTable with customer info, and
' then modify, delete, and add rows.
Dim table As DataTable = GetCustomers()
' Row 0 is unmodified.
' Row 1 is modified.
' Row 2 is deleted.
' Row 3 is added.
table.Rows(1)("Name") = "Sydney"
table.Rows(2).Delete()
Dim row As DataRow = table.NewRow
row("ID") = 3
row("Name") = "Melony"
table.Rows.Add(row)
' Note that the code doesn't call
' table.AcceptChanges()
Return table
End Function
Private Sub HandleRowChanging(ByVal sender As Object, _
ByVal e As System.Data.DataRowChangeEventArgs)
Console.WriteLine( _
"RowChanging event: ID = {0}, action = {1}", e.Row("ID"), e.Action)
End Sub
开发者ID:VB.NET开发者,项目名称:System.Data,代码行数:231,代码来源:DataTable.Load
示例4: Main
Sub Main()
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:
table = GetIntegerTable()
Dim reader As New DataTableReader(GetStringTable())
table.Load(reader, LoadOption.OverwriteChanges, _
AddressOf FillErrorHandler)
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
开发者ID:VB.NET开发者,项目名称:System.Data,代码行数:82,代码来源:DataTable.Load
注:本文中的System.Data.DataTable.Load方法示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论