我需要遍历gridview才能找到与不同表中的项匹配的记录(SQL中)。是循环gridview并在每个循环中调用SQL过程以查找匹配记录的最佳方法?或者我应该将整个sql数据库拉到数据表中,并在我的gridview循环中查询数据集?将数据集保存在内存或多个SQL查询中
'find source ID based on make/model/serial No combination.
Dim cSource As New clsSource()
Dim ds As DataSet = cSource.GetSources()
Dim found As Boolean = False
'populate db datatables
Dim dt As DataTable = ds.Tables(0)
Dim rows As Integer = gwResults.Rows.Count()
For Each row As GridViewRow In gwResults.Rows
'move through rows and check data in each row against the dataset
'1 - make
For Each dataRow As DataRow In dt.Rows
found = False
If dataRow("manufacturerName") = row.Cells(1).Text Then
If dataRow("modelName") = row.Cells(2).Text Then
If dataRow("serialNo") = row.Cells(3).Text Then
found = True
End If
End If
End If
'display results
If found Then
lblResults.Text += row.Cells(1).Text & "/" & row.Cells(2).Text & "/" & row.Cells(3).Text & " found"
Else
lblResults.Text += row.Cells(1).Text & "/" & row.Cells(2).Text & "/" & row.Cells(3).Text & " not found "
End If
Next
Next
也考虑优化'SQL'加载。多个查询可能比单个长时间运行的查询更重要。 – JNK 2011-04-19 19:54:13
谢谢。我尝试了一个单一的查询 - 它约18K的记录,但我必须多次循环(对于每个网格视图行),需要一段时间才能完成,我试图优化时间。我即将编辑上面的帖子,请看看。 – 2011-04-19 19:56:15
我是否正确地认为你试图匹配两个数据集中的记录?如果是这样,你有没有考虑过在SQL中进行连接? – 2011-04-20 12:43:55