如何在DataSet中查找记录?
How to find a record in DataSet?
我正在尝试在 Dataset
中查找特定记录,这是由查询结果填充的,例如:
Dim ds As New DataSet
Dim query = "SELECT * FROM tax ORDER BY id"
MyAdapter = New MySqlDataAdapter(query, my connection string here)
MyAdapter.Fill(ds)
现在我有另一个 Dataset
,其内容与 table 相同,但属于另一个数据库。我正在尝试获取字段 hash
,这允许我比较此 hash
行的所有字段。实际上我所做的是创建一个这样的循环:
If ds.Tables(0).Rows.Count > 0 Then
Dim x As Integer = 0
For x = 0 To ds.Tables(0).Rows.Count - 1
Dim local_hash = ds.Tables(0).Rows(x).Item("hash")
Dim web_hash = ds2.Tables(0).Rows(x).Item("hash") 'This is another dataset as I explained in the comment above.
If local_hash = web_hash 'compare if the hash is equal
现在这段代码因为一个明显的原因不起作用,两个数据库的索引在循环中是不同的然后我永远找不到引用循环索引的 hash
字段数据集。所以我想问一下是否有像.Find
这样的方法或其他方法可以用来避免这种情况。
您可以从 DataTable
select 获取符合条件的行数组:
Dim dtLocal = ds.Tables(0)
Dim dtWeb = ds2.Tables(0)
Dim local_hash As String '??
Dim search As String = "Hash = '{0}'"
For n = 0 To dtLocal.Rows.Count-1
local_hash = dtLocal.Rows(n).Item("hash").ToString
' get matching rows
Dim webRows = dtWeb.Select(String.Format(search, local_hash))
'iterate matches
For Each r As DataRow In webRows
' do wonderful things
Next
Next
您可能需要尝试使用搜索字符串格式,因为它不清楚散列是什么。听起来像字符串。
一种替代方法是使用 DataView
或者另一种方法是使用一个 DataTable
作为 "master" 并使用 "hash" 重建另一个当前行作为 SQL WHERE 子句。评论中的 DataView 版本如下所示:
Dim dvW As New DataView(dtWeb, "", "hash", DataViewRowState.CurrentRows)
For n = 0 To dtLocal.Rows.Count - 1
local_hash = dtLocal.Rows(n).Item("hash").ToString
' get matching rows
Dim webRows = dvW.FindRows(local_hash)
' iterate matches
' possibly panic if more than one row
For Each dvr As DataRowView In webRows
' do wonderful things
Next
Next
如果您使用 Find
而不是 FindRows
,它将 return 行的索引。 FindRows 会捕获其他问题,例如 Guid/hash 被多次使用。
使用 400+ "master" 行和 4000+ "detail" 行,并且只是迭代 master 来收集详细信息 ID,每个都花费大约相同的时间:[=17= 260ms ] vs 376 完全重建 table vs 253 使用 DataView.FindRows
。
我正在尝试在 Dataset
中查找特定记录,这是由查询结果填充的,例如:
Dim ds As New DataSet
Dim query = "SELECT * FROM tax ORDER BY id"
MyAdapter = New MySqlDataAdapter(query, my connection string here)
MyAdapter.Fill(ds)
现在我有另一个 Dataset
,其内容与 table 相同,但属于另一个数据库。我正在尝试获取字段 hash
,这允许我比较此 hash
行的所有字段。实际上我所做的是创建一个这样的循环:
If ds.Tables(0).Rows.Count > 0 Then
Dim x As Integer = 0
For x = 0 To ds.Tables(0).Rows.Count - 1
Dim local_hash = ds.Tables(0).Rows(x).Item("hash")
Dim web_hash = ds2.Tables(0).Rows(x).Item("hash") 'This is another dataset as I explained in the comment above.
If local_hash = web_hash 'compare if the hash is equal
现在这段代码因为一个明显的原因不起作用,两个数据库的索引在循环中是不同的然后我永远找不到引用循环索引的 hash
字段数据集。所以我想问一下是否有像.Find
这样的方法或其他方法可以用来避免这种情况。
您可以从 DataTable
select 获取符合条件的行数组:
Dim dtLocal = ds.Tables(0)
Dim dtWeb = ds2.Tables(0)
Dim local_hash As String '??
Dim search As String = "Hash = '{0}'"
For n = 0 To dtLocal.Rows.Count-1
local_hash = dtLocal.Rows(n).Item("hash").ToString
' get matching rows
Dim webRows = dtWeb.Select(String.Format(search, local_hash))
'iterate matches
For Each r As DataRow In webRows
' do wonderful things
Next
Next
您可能需要尝试使用搜索字符串格式,因为它不清楚散列是什么。听起来像字符串。
一种替代方法是使用 DataView
或者另一种方法是使用一个 DataTable
作为 "master" 并使用 "hash" 重建另一个当前行作为 SQL WHERE 子句。评论中的 DataView 版本如下所示:
Dim dvW As New DataView(dtWeb, "", "hash", DataViewRowState.CurrentRows)
For n = 0 To dtLocal.Rows.Count - 1
local_hash = dtLocal.Rows(n).Item("hash").ToString
' get matching rows
Dim webRows = dvW.FindRows(local_hash)
' iterate matches
' possibly panic if more than one row
For Each dvr As DataRowView In webRows
' do wonderful things
Next
Next
如果您使用 Find
而不是 FindRows
,它将 return 行的索引。 FindRows 会捕获其他问题,例如 Guid/hash 被多次使用。
使用 400+ "master" 行和 4000+ "detail" 行,并且只是迭代 master 来收集详细信息 ID,每个都花费大约相同的时间:[=17= 260ms ] vs 376 完全重建 table vs 253 使用 DataView.FindRows
。