简体   繁体   English

vb.net循环查询结果

[英]vb.net cycle through query results

I am familiar with the VB6 ADO way of dealing with SQL queries and looping through the record set results. 我熟悉VB6 ADO处理SQL查询和遍历记录集结果的方式。

However, what is the correct way to query a server, cycle through the results, and dispose of my query in VB.Net? 但是,查询服务器,循环搜索结果以及在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

I suspect the problem has to do with how you manage your gconIntegration connection. 我怀疑问题与您如何管理gconIntegration连接有关。 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. 最好从池中获取“新”连接,然后让.Net为您担心。

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. 同样,您的通用“ GetSQLTable”代码缺少重要部分:它不允许设置参数,这告诉我您是将它们直接构建到查询字符串中。 That's a recipe for disaster: it will lead to Sql injection security holes. 那是灾难的秘诀:它将导致Sql注入安全漏洞。

One more thing: don't set objects to Nothing in .Net. 还有一件事:不要在.Net中将对象设置为Nothing 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: 这是从数据表中拉回数据表的正常方法:

Function GetSomeData(ByVal Table2ID As Integer)
    Dim result As New DataTable

    Dim sql As String = "SELECT Column1,Column2 FROM [Table1] WHERE Table2ID= @Table2ID"

    Using cn As New SqlConnection( GetConnectionString() ), _
    Using cmd As New SqlCommand(sql, cn)

        cmd.Parameters.Add("@Table2ID", SqlDbType.Int).Value = Table2ID

        Using rdr As SqlDataReader = cmd.ExecuteReader()
           result.Load(rdr)
        End Using
    End Using
    return result
End Function

Some notes on that code: 关于该代码的一些注释:

  • The Using statement will guarantee that the associated object is disposed at the corresponding End Using . Using语句将确保将关联的对象放置在相应的End Using
  • The query parameters are kept strongly typed, and never substituted directly into the query string, even when they are transmitted to the server. 查询参数保持强类型,并且即使将它们传输到服务器,也绝不能直接替换为查询字符串。 Sql Data and Sql Code never mix. Sql Data和Sql Code从不混用。
  • You do need a separate function for each query you need to send. 您确实需要为每个要发送的查询提供单独的功能。 This is really a good thing, as it leads into building a strongly-typed interface for your database. 这确实是一件好事,因为它可以为数据库建立一个强类型的接口。 Ideally all of these functions are in the same class, and the GetConnectionString function is private to that class. 理想情况下,所有这些函数都在同一类中,并且GetConnectionString函数对该类是私有的。 No database access happens outside of this data layer. 在此数据层之外没有数据库访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM