本文整理匯總了VB.NET中System.Data.DataTable類的典型用法代碼示例。如果您正苦於以下問題:VB.NET DataTable類的具體用法?VB.NET DataTable怎麽用?VB.NET DataTable使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了DataTable類的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的VB.NET代碼示例。
示例1: MakeDataTables
' Put the next line into the Declarations section.
private dataSet As DataSet
Private Sub MakeDataTables()
' Run all of the functions.
MakeParentTable()
MakeChildTable()
MakeDataRelation()
BindToDataGrid()
End Sub
Private Sub MakeParentTable()
' Create a new DataTable.
Dim table As New DataTable("ParentTable")
' Declare variables for DataColumn and DataRow objects.
Dim column As DataColumn
Dim row As DataRow
' Create new DataColumn, set DataType, ColumnName
' and add to DataTable.
column = New DataColumn()
column.DataType = System.Type.GetType("System.Int32")
column.ColumnName = "id"
column.ReadOnly = True
column.Unique = True
' Add the Column to the DataColumnCollection.
table.Columns.Add(column)
' Create second column.
column = New DataColumn()
column.DataType = System.Type.GetType("System.String")
column.ColumnName = "ParentItem"
column.AutoIncrement = False
column.Caption = "ParentItem"
column.ReadOnly = False
column.Unique = False
' Add the column to the table.
table.Columns.Add(column)
' Make the ID column the primary key column.
Dim PrimaryKeyColumns(0) As DataColumn
PrimaryKeyColumns(0)= table.Columns("id")
table.PrimaryKey = PrimaryKeyColumns
' Instantiate the DataSet variable.
dataSet = New DataSet()
' Add the new DataTable to the DataSet.
dataSet.Tables.Add(table)
' Create three new DataRow objects and add
' them to the DataTable
Dim i As Integer
For i = 0 to 2
row = table.NewRow()
row("id") = i
row("ParentItem") = "ParentItem " + i.ToString()
table.Rows.Add(row)
Next i
End Sub
Private Sub MakeChildTable()
' Create a new DataTable.
Dim table As New DataTable("childTable")
Dim column As DataColumn
Dim row As DataRow
' Create first column and add to the DataTable.
column = New DataColumn()
column.DataType= System.Type.GetType("System.Int32")
column.ColumnName = "ChildID"
column.AutoIncrement = True
column.Caption = "ID"
column.ReadOnly = True
column.Unique = True
' Add the column to the DataColumnCollection.
table.Columns.Add(column)
' Create second column.
column = New DataColumn()
column.DataType= System.Type.GetType("System.String")
column.ColumnName = "ChildItem"
column.AutoIncrement = False
column.Caption = "ChildItem"
column.ReadOnly = False
column.Unique = False
table.Columns.Add(column)
' Create third column.
column = New DataColumn()
column.DataType= System.Type.GetType("System.Int32")
column.ColumnName = "ParentID"
column.AutoIncrement = False
column.Caption = "ParentID"
column.ReadOnly = False
column.Unique = False
table.Columns.Add(column)
dataSet.Tables.Add(table)
' Create three sets of DataRow objects, five rows each,
' and add to DataTable.
Dim i As Integer
For i = 0 to 4
row = table.NewRow()
row("childID") = i
row("ChildItem") = "Item " + i.ToString()
row("ParentID") = 0
table.Rows.Add(row)
Next i
For i = 0 to 4
row = table.NewRow()
row("childID") = i + 5
row("ChildItem") = "Item " + i.ToString()
row("ParentID") = 1
table.Rows.Add(row)
Next i
For i = 0 to 4
row = table.NewRow()
row("childID") = i + 10
row("ChildItem") = "Item " + i.ToString()
row("ParentID") = 2
table.Rows.Add(row)
Next i
End Sub
Private Sub MakeDataRelation()
' DataRelation requires two DataColumn
' (parent and child) and a name.
Dim parentColumn As DataColumn = _
dataSet.Tables("ParentTable").Columns("id")
Dim childColumn As DataColumn = _
dataSet.Tables("ChildTable").Columns("ParentID")
Dim relation As DataRelation = new _
DataRelation("parent2Child", parentColumn, childColumn)
dataSet.Tables("ChildTable").ParentRelations.Add(relation)
End Sub
Private Sub BindToDataGrid()
' Instruct the DataGrid to bind to the DataSet, with the
' ParentTable as the topmost DataTable.
DataGrid1.SetDataBinding(dataSet,"ParentTable")
End Sub
示例2: Program
' 導入命名空間
Imports System.Data
Class Program
Public Shared Sub Main(args As String())
' Create two tables and add them into the DataSet
Dim orderTable As DataTable = CreateOrderTable()
Dim orderDetailTable As DataTable = CreateOrderDetailTable()
Dim salesSet As New DataSet()
salesSet.Tables.Add(orderTable)
salesSet.Tables.Add(orderDetailTable)
' Set the relations between the tables and create the related constraint.
salesSet.Relations.Add("OrderOrderDetail", orderTable.Columns("OrderId"), orderDetailTable.Columns("OrderId"), True)
Console.WriteLine("After creating the foreign key constriant, you will see the following error if inserting order detail with the wrong OrderId: ")
Try
Dim errorRow As DataRow = orderDetailTable.NewRow()
errorRow(0) = 1
errorRow(1) = "O0007"
orderDetailTable.Rows.Add(errorRow)
Catch e As Exception
Console.WriteLine(e.Message)
End Try
Console.WriteLine()
' Insert the rows into the table
InsertOrders(orderTable)
InsertOrderDetails(orderDetailTable)
Console.WriteLine("The initial Order table.")
ShowTable(orderTable)
Console.WriteLine("The OrderDetail table.")
ShowTable(orderDetailTable)
' Use the Aggregate-Sum on the child table column to get the result.
Dim colSub As New DataColumn("SubTotal", GetType([Decimal]), "Sum(Child.LineTotal)")
orderTable.Columns.Add(colSub)
' Compute the tax by referencing the SubTotal expression column.
Dim colTax As New DataColumn("Tax", GetType([Decimal]), "SubTotal*0.1")
orderTable.Columns.Add(colTax)
' If the OrderId is 'Total', compute the due on all orders; or compute the due on this order.
Dim colTotal As New DataColumn("TotalDue", GetType([Decimal]), "IIF(OrderId='Total',Sum(SubTotal)+Sum(Tax),SubTotal+Tax)")
orderTable.Columns.Add(colTotal)
Dim row As DataRow = orderTable.NewRow()
row("OrderId") = "Total"
orderTable.Rows.Add(row)
Console.WriteLine("The Order table with the expression columns.")
ShowTable(orderTable)
Console.WriteLine("Press any key to exit.....")
Console.ReadKey()
End Sub
Private Shared Function CreateOrderTable() As DataTable
Dim orderTable As New DataTable("Order")
' Define one column.
Dim colId As New DataColumn("OrderId", GetType([String]))
orderTable.Columns.Add(colId)
Dim colDate As New DataColumn("OrderDate", GetType(DateTime))
orderTable.Columns.Add(colDate)
' Set the OrderId column as the primary key.
orderTable.PrimaryKey = New DataColumn() {colId}
Return orderTable
End Function
Private Shared Function CreateOrderDetailTable() As DataTable
Dim orderDetailTable As New DataTable("OrderDetail")
' Define all the columns once.
Dim cols As DataColumn() = {New DataColumn("OrderDetailId", GetType(Int32)), New DataColumn("OrderId", GetType([String])), New DataColumn("Product", GetType([String])), New DataColumn("UnitPrice", GetType([Decimal])), New DataColumn("OrderQty", GetType(Int32)), New DataColumn("LineTotal", GetType([Decimal]), "UnitPrice*OrderQty")}
orderDetailTable.Columns.AddRange(cols)
orderDetailTable.PrimaryKey = New DataColumn() {orderDetailTable.Columns("OrderDetailId")}
Return orderDetailTable
End Function
Private Shared Sub InsertOrders(orderTable As DataTable)
' Add one row once.
Dim row1 As DataRow = orderTable.NewRow()
row1("OrderId") = "O0001"
row1("OrderDate") = New DateTime(2013, 3, 1)
orderTable.Rows.Add(row1)
Dim row2 As DataRow = orderTable.NewRow()
row2("OrderId") = "O0002"
row2("OrderDate") = New DateTime(2013, 3, 12)
orderTable.Rows.Add(row2)
Dim row3 As DataRow = orderTable.NewRow()
row3("OrderId") = "O0003"
row3("OrderDate") = New DateTime(2013, 3, 20)
orderTable.Rows.Add(row3)
End Sub
Private Shared Sub InsertOrderDetails(orderDetailTable As DataTable)
' Use an Object array to insert all the rows .
' Values in the array are matched sequentially to the columns, based on the order in which they appear in the table.
Dim rows As [Object]() = {New [Object]() {1, "O0001", "Mountain Bike", 1419.5, 36}, New [Object]() {2, "O0001", "Road Bike", 1233.6, 16}, New [Object]() {3, "O0001", "Touring Bike", 1653.3, 32}, New [Object]() {4, "O0002", "Mountain Bike", 1419.5, 24}, New [Object]() {5, "O0002", "Road Bike", 1233.6, 12}, New [Object]() {6, "O0003", "Mountain Bike", 1419.5, 48}, _
New [Object]() {7, "O0003", "Touring Bike", 1653.3, 8}}
For Each row As [Object]() In rows
orderDetailTable.Rows.Add(row)
Next
End Sub
Private Shared Sub ShowTable(table As DataTable)
For Each col As DataColumn In table.Columns
Console.Write("{0,-14}", col.ColumnName)
Next
Console.WriteLine()
For Each row As DataRow In table.Rows
For Each col As DataColumn In table.Columns
If col.DataType.Equals(GetType(DateTime)) Then
Console.Write("{0,-14:d}", row(col))
ElseIf col.DataType.Equals(GetType([Decimal])) Then
Console.Write("{0,-14:C}", row(col))
Else
Console.Write("{0,-14}", row(col))
End If
Next
Console.WriteLine()
Next
Console.WriteLine()
End Sub
End Class