[转]VB.NET DataTable Select Function

本文转自:https://www.dotnetperls.com/datatable-select-vbnet

VB.NET DataTable Select Function
This VB.NET example program uses the DataTable type and its Select Function. Select searches for matching rows with a query.
DataTable Select. A DataTable stores rows and columns. It is a container of other data. But it also provides search functionality. With the Select method, we query a DataTable for rows that match a condition.
DataTable



Example. This program first creates a DataTable with the name "Players." It then adds two columns—each row will store a "Size" and "Sex" value. We call Rows.Add five times to populate the collection with data.

Then: We call the DataTable Select Function. This returns an array of DataRow instances.
DataRow

Query: We use the query (Size >= 230 AND Sex = "m"). So only data rows with a Size greater than 229 and a Sex of "m" are returned.
VB.NET program that uses DataTable, Select

Module Module1

    Sub Main()
        ' Create a table.
        Dim table As DataTable = New DataTable("Players")

        ' Add two columns.
        table.Columns.Add(New DataColumn("Size", GetType(Integer)))
        table.Columns.Add(New DataColumn("Sex", GetType(Char)))

        ' Add five rows.
        table.Rows.Add(100, "f"c)
        table.Rows.Add(235, "f"c)
        table.Rows.Add(250, "m"c)
        table.Rows.Add(310, "m"c)
        table.Rows.Add(150, "m"c)

        ' Get players above 230 size with "m" sex.
        Dim result() As DataRow = table.Select("Size >= 230 AND Sex = 'm'")

        ' Loop and display.
        For Each row As DataRow In result
            Console.WriteLine("{0}, {1}", row(0), row(1))
        Next
    End Sub

End Module

Output

250, m
310, m
DateTime. Next, we use the Select Function with a DateTime. We create a new DataTable storing Widget model information. We populate it with three rows. These contain the ID of the widget and the DateTime the widget was built.

Then: We pass the query string containing a DateTime substring to the Select Function.

Note: For using a DateTime query, we can use the numeric comparison operators. We must surround the date with pound "#" symbols.
VB.NET program that uses Select, date

Module Module1

    Sub Main()
        ' Create a table.
        Dim table As DataTable = New DataTable("Widgets")

        ' Add two columns.
        table.Columns.Add(New DataColumn("ID", GetType(Integer)))
        table.Columns.Add(New DataColumn("Date", GetType(DateTime)))

        ' Add rows.
        table.Rows.Add(100, New DateTime(2001, 1, 1))
        table.Rows.Add(200, New DateTime(2002, 1, 1))
        table.Rows.Add(300, New DateTime(2003, 1, 1))

        ' Get rows more recent than 6/1/2001.
        Dim result() As DataRow = table.Select("Date > #6/1/2001#")

        ' Loop.
        For Each row As DataRow In result
            Console.WriteLine(row(0))
        Next
    End Sub

End Module

Output

200
300
Or. In the examples, we have seen the "AND" operator. We have also done numeric comparisons and date comparisons. Another supported operator for Select is the "OR" operator. This operator is used in the same way as in an SQL query.


Summary. A DataTable is more than a container for data objects. It provides functionality that helps you search for matching rows. The syntax is like that of an SQL query. But no database is ever opened or used.


© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.
Home
Search
Home
Dot Net Perls

 

posted on 2019-10-06 21:15  freeliver54  阅读(1636)  评论(0编辑  收藏  举报

导航