vb.net循环查询结果
我熟悉处理 SQL 查询和循环记录集结果的 VB6 ADO 方式。
但是,在 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
I am familiar with the VB6 ADO way of dealing with SQL queries and looping through the record set results.
However, what is the correct way to query a server, cycle through the results, and dispose of my query in VB.Net? All the ways I have been using seem to be unstable and crash randomly.
I have been using the following code:
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
And using it like this:
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我怀疑问题与您管理
gconIntegration
连接的方式有关。 您太努力地尝试继续使用相同的连接。 看看它住在哪里会很有帮助。最好从池中获取“新”连接,并让 .Net 为您操心。
另外,您的通用“GetSQLTable”代码缺少一个重要部分:它不允许设置参数,这告诉我您正在将它们直接构建到查询字符串中。 这是灾难的根源:它将导致 Sql 注入安全漏洞。
还有一件事:不要在 .Net 中将对象设置为
Nothing
。 如果需要,要么将它们丢弃,要么让它们自行超出范围。以下是我从数据表中提取数据表的常规方法:
关于该代码的一些注释:
Using
语句将保证关联的对象在相应的End using
处被释放。I suspect the problem has to do with how you manage your
gconIntegration
connection. You're trying too hard to keep using that same connection. It would be helpful to see where it lives.Better to get "new" connections from the pool and let .Net worry about it for you.
Also, your generic "GetSQLTable" code is missing an important part: it makes no allowance for setting parameters, which tells me you're building them directly into your query strings. That's a recipe for disaster: it will lead to Sql injection security holes.
One more thing: don't set objects to
Nothing
in .Net. Either dispose them if needed or let them fall out of scope on their own.Here's my normal method for pulling back a datatable from a datatable:
Some notes on that code:
Using
statement will guarantee that the associated object is disposed at the correspondingEnd Using
.