vb.net 检查数据集是否包含特定值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3830876/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if dataset contains a specific value
提问by Yustme
How can i check if a dataset contains a specific value? It's crazy that no one has done this before. Couldn't find it on the net!!!
如何检查数据集是否包含特定值?很疯狂,以前没有人这样做过。网上没找到!!!
回答by pjnovas
You mean go through the entire dataset tables, columns and rows?
你的意思是遍历整个数据集表、列和行?
Here is something that could help you:
这里有一些可以帮助你的东西:
Dim valueToSearch as String = "some text"
For Each dTable As DataTable In ds.Tables
For Each dRow As DataRow In dTable.Rows
For index As Integer = 0 To dTable.Columns.Count - 1
Convert.ToString(dRow(index)).Contains(valueToSearch)
Next
Next
Next
回答by Vivek Singh
Suppose I have Dataset DsStu containing Table Student with Columns [Rollno,Name,Branch].
假设我有包含带有列 [Rollno,Name,Branch] 的表 Student 的数据集 DsStu。
Case 1I want Name of student whose Rollno is 15.
案例 1我想要 Rollno 为 15 的学生的姓名。
Dim answer As String = ""
Dim SerchRows() As Data.DataRow
SerchRows = DsStu.Tables(0).Select("Rollno = '15'")
answer = ""
For k As Integer = 0 To SerchRows.Length - 1
If answer = "" then
answer = SerchRows(k).Item("Name")
Else
answer = answer & Vbnewline & SerchRows(k).Item("Name")
End If
Next
MsgBox(" " & answer)
Case 2I want Name of all student whose Rollno is greater than equal to 15 and Branch is Electrical.
案例 2我想要所有 Rollno 大于等于 15 且 Branch 为 Electrical 的学生的姓名。
Dim answer As String = ""
Dim SerchRows() As Data.DataRow
SerchRows = DsStu.Tables(0).Select("Rollno >= '15' And Branch = 'Electrical'")
answer = ""
For k As Integer = 0 To SerchRows.Length - 1
If answer = "" then
answer = SerchRows(k).Item("Name")
Else
answer = answer & Vbnewline & SerchRows(k).Item("Name")
End If
Next
MsgBox(" " & answer)
回答by Hasan
If you're using a BindingSource use the "Find" method: http://msdn.microsoft.com/en-us/library/ms158165.aspx
如果您使用的是 BindingSource,请使用“查找”方法:http: //msdn.microsoft.com/en-us/library/ms158165.aspx
So if it returns -1 it's not there and otherwise it will return the position.
因此,如果它返回 -1,则它不存在,否则它将返回位置。