Some DataTable Functions in VB.net
Create table in VB.net:
Dim table As New DataTable("Orders")
table.Columns.Add("OrderID", GetType(Int32))
table.Columns.Add("OrderQuantity", GetType(Int32))
table.Columns.Add("CompanyName", GetType(String))
table.Columns.Add("Date", GetType(DateTime))
Dim newRow As DataRow = table.NewRow()
newRow("OrderID") = 1
newRow("OrderQuantity") = 3
newRow("CompanyName") = "NewCompanyName"
newRow("Date") = "2012, 1, 31"
' Add the row to the rows collection.
table.Rows.Add(newRow)
Plain Select
Private Sub GetAllRows()
' Get the DataTable of a DataSet.
Dim table As DataTable = DataSet1.Tables("Shippers")
Dim rows() As DataRow = table.Select()
Dim i As Integer
' Print the value one column of each DataRow.
For i = 0 to rows.GetUpperBound(0)
Console.WriteLine(rows(i)("Name"))
Next i
End Sub
Select with Expression
Private Sub GetFilteredRows()
Dim table As DataTable = DataSet1.Tables("Books")
' Presuming the DataTable has a column named PublishedDate.
Dim expression As String
expression = "PublishedDate > #1/1/10#"
Dim foundRows() As DataRow
' Use the Select method to find all rows matching the filter.
foundRows = table.Select(expression)
Dim i As Integer
' Print column 0 of each returned row.
For i = 0 to foundRows.GetUpperBound(0)
Console.WriteLine(foundRows(i)(0))
Next i
End Sub
Multiple Filters
Dim foundRows() As DataRow
foundRows() = table.Select("Col1 = 'foo' AND Col2 = 'bar'")
Sort
Dim foundRows() As DataRow
' Sort descending by column named Column1
Dim sortOrder As String = "Col1 ASC"
foundRows() = table.Select("Col1 = 'foo' AND Col2 = 'bar'", sortOrder)
Create table in VB.net:
Dim table As New DataTable("Orders")
table.Columns.Add("OrderID", GetType(Int32))
table.Columns.Add("OrderQuantity", GetType(Int32))
table.Columns.Add("CompanyName", GetType(String))
table.Columns.Add("Date", GetType(DateTime))
Dim newRow As DataRow = table.NewRow()
newRow("OrderID") = 1
newRow("OrderQuantity") = 3
newRow("CompanyName") = "NewCompanyName"
newRow("Date") = "2012, 1, 31"
' Add the row to the rows collection.
table.Rows.Add(newRow)
Plain Select
Private Sub GetAllRows()
' Get the DataTable of a DataSet.
Dim table As DataTable = DataSet1.Tables("Shippers")
Dim rows() As DataRow = table.Select()
Dim i As Integer
' Print the value one column of each DataRow.
For i = 0 to rows.GetUpperBound(0)
Console.WriteLine(rows(i)("Name"))
Next i
End Sub
Select with Expression
Private Sub GetFilteredRows()
Dim table As DataTable = DataSet1.Tables("Books")
' Presuming the DataTable has a column named PublishedDate.
Dim expression As String
expression = "PublishedDate > #1/1/10#"
Dim foundRows() As DataRow
' Use the Select method to find all rows matching the filter.
foundRows = table.Select(expression)
Dim i As Integer
' Print column 0 of each returned row.
For i = 0 to foundRows.GetUpperBound(0)
Console.WriteLine(foundRows(i)(0))
Next i
End Sub
Multiple Filters
Dim foundRows() As DataRow
foundRows() = table.Select("Col1 = 'foo' AND Col2 = 'bar'")
Sort
Dim foundRows() As DataRow
' Sort descending by column named Column1
Dim sortOrder As String = "Col1 ASC"
foundRows() = table.Select("Col1 = 'foo' AND Col2 = 'bar'", sortOrder)
No comments:
Post a Comment