我需要遍歷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