织梦CMS - 轻松建站从此开始!

欧博ABG-会员注册-官网网址

欧博娱乐DataTable Select Function

时间:2025-08-29 01:12来源: 作者:admin 点击: 5 次
DataRowDataTable SelectA DataTable stores rows and columns of certain types of data. It contains other data—it is a collection. But the DataTabl

DataRow

DataTable Select

A DataTable stores rows and columns of certain types of data. It contains other data—it is a collection. But the DataTable also provides search functionality.

With the Select Function, we query a DataTable for rows that match a condition. We can then loop over the resulting array of DataRow objects.

First example

We create a DataTable with the name "Players." We add 2 columns—each row will store a "Size" and "Team" value. We call Rows.Add 5 times to populate the collection with data.

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

Info We use a query that tests Size and Team. Only data rows with a Size greater than 229 and a Team of "b" are returned.

Module Module1 Sub Main() ' Create a table. Dim table As DataTable = New DataTable("Players") ' Add 2 columns. table.Columns.Add(New DataColumn("Size", GetType(Integer))) table.Columns.Add(New DataColumn("Team", GetType(Char))) ' Add 5 rows. table.Rows.Add(100, "a"c) table.Rows.Add(235, "a"c) table.Rows.Add(250, "b"c) table.Rows.Add(310, "b"c) table.Rows.Add(150, "b"c) ' Get players above 230 size with "b" team. Dim result() As DataRow = table.Select("Size >= 230 AND Team = 'b'") ' Loop and display. For Each row As DataRow In result Console.WriteLine("{0}, {1}", row(0), row(1)) Next End Sub End Module250, b 310, bDateTime example

Next we use the Select Function with a DateTime. We create a new DataTable storing Widget model information. We populate it with three rows.

Detail The rows contain the ID of the widget and the DateTime the widget was built. For DateTimes, we use the constructor.

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.

Module Module1 Sub Main() ' Create a table. Dim table As DataTable = New DataTable("Widgets") ' Add 2 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 Module200 300EvaluateException

We must specify a known column name in the Select expression. If we use a nonexistent column name like "X" here, an EvaluateException is thrown.

Module Module1 Sub Main() Dim table As DataTable = New DataTable("Players") ' We must use known columns, or an EvaluateException will occur. Dim result() As DataRow = table.Select("X") End Sub End ModuleUnhandled Exception: System.Data.EvaluateException: Cannot find column [X]. at System.Data.NameNode.Bind(DataTable table, List`1 list) at System.Data.DataExpression.Bind(DataTable table) at System.Data.DataExpression..ctor(DataTable table, String expression, Type type) at System.Data.DataTable.Select(String filterExpression)Notes, syntax

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.

Info This operator is used in the same way as in an SQL query. Select() makes DataTables act like small databases.

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.

(责任编辑:)
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:
发布者资料
查看详细资料 发送留言 加为好友 用户等级: 注册时间:2025-08-30 20:08 最后登录:2025-08-30 20:08
栏目列表
推荐内容