【发布时间】:2009-02-27 20:50:38
【问题描述】:
我熟悉 VB6 ADO 处理 SQL 查询和循环遍历记录集结果的方式。
但是,查询服务器、循环查看结果以及在 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
【问题讨论】: