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