2
我熟悉VB6 ADO處理SQL查詢和循環記錄集結果的方式。vb.net循環查詢結果
但是,什麼是正確的方式來查詢服務器,遍歷結果,並在VB.Net中處理我的查詢?我一直使用的所有方式似乎都不穩定,並隨機崩潰。
我一直在使用下面的代碼:
Public Function GetSQLTable(ByVal strSQL As String) As DataTable
Dim table As New DataTable
Dim adapt As SqlDataAdapter
Try
adapt = New SqlDataAdapter(strSQL, gconIntegration)
adapt.Fill(table)
Catch ex As Exception
LogError("GetSQLTable: " & ex.ToString(), "SQL: " & strSQL)
End Try
Return table
End Function
,並用它是這樣的:
Dim dt As DataTable
Dim lngRow As Long
Dim current As DataRow
Dim lngContact As long
Try
dt = GetSQLTable(strSQL)
For lngRow = 0 To dt.Rows.Count - 1
current = dt.Rows.Item(lngRow)
lngContact = current.Item("indvid")
DoSomething(lngContact)
Next
Catch ex As Exception
LogError("FindContact: " & ex.ToString(), "SQL: " & strSQL)
lngContact = -1
Finally
current = nothing
dt = nothing
gconIntegration是一個全局連接。所以你說我所有閱讀和寫作的單一連接並不是最佳途徑? – 2009-02-27 21:11:56