是否可以将一个连接字符串用于多个查询

时间:2017-12-19 08:55:02

标签: sql-server vb.net

如何优化此代码,我通过购买服务器ram来优化我的执行错误,但我希望我的代码优化到....并且我可以重用我的连接吗?

我有一个public sub sqlconnect(),其中包含我的连接代码

sqlcon = new sqlconnection(cstring);

我可以使用sqlconnect处理下面的所有查询而不再调用它吗?或者我应该使用sqlconnect然后只使用sqlcon.close()?

你可以在下面看到我使用了太多的sqlconenction ...

Public Class Tracking
    Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
        Try
            Dim airbill, branchcode, t_sender, receiver, setpackagetype, getpackagetype, setdestination, getdestination, Setbranchid, getbranchid, settrackstats, dateuploaded As String
            sqlconnect()
            sqltable = New DataTable
            sqladapter = New SqlClient.SqlDataAdapter("SELECT * FROM AIRBILLS WHERE  AirbillNo='" & txttrackor.Text & "' and TrackingNo = '" & txttrackcode.Text & "'", sqlcon)
            sqladapter.Fill(sqltable)
            settrackstats = sqltable.Rows(0)("TrackingstatusID")
            airbill = sqltable.Rows(0)("AirbillNo")
            branchcode = sqltable.Rows(0)("TrackingNo")
            t_sender = sqltable.Rows(0)("Sender")
            receiver = sqltable.Rows(0)("Addressee")
            setpackagetype = sqltable.Rows(0)("ServiceID")
            Setbranchid = sqltable.Rows(0)("BranchID")
            dateuploaded = sqltable.Rows(0)("DateUploaded")
            Label17.Text = dateuploaded
            'get destination
            If IsDBNull(sqltable.Rows(0)("destinationID")) Then
                getdestination = ""
                Label13.Text = getdestination
            Else
                setdestination = sqltable.Rows(0)("destinationID")
                sqlconnect()
                sqltable = New DataTable
                sqladapter = New SqlClient.SqlDataAdapter("SELECT * FROM DESTINATION where ID = '" & setdestination & "'", sqlcon)
                sqladapter.Fill(sqltable)
                getdestination = sqltable.Rows(0)("DestinationName")
                Label13.Text = getdestination
                sqlcon.Close()
            End If
            sqlcon.Close()
            'get trackignstatus
            ListBox1.Items.Clear()

            For Each track As DataRow In sqltable.Rows
                sqltable = New DataTable
                sqladapter = New SqlClient.SqlDataAdapter("SELECT * FROM TRACKINGSTATUS WHERE  ID = '" & settrackstats & "'", sqlcon)
                sqladapter.Fill(sqltable)
                ListBox1.Items.Add(sqltable.Rows(0)("TrackingStatus"))
            Next

            'get the service name of the tracking
            sqlconnect()
            sqltable = New DataTable
            sqladapter = New SqlClient.SqlDataAdapter("SELECT * FROM SERVICE where ID = '" & setpackagetype & "'", sqlcon)
            sqladapter.Fill(sqltable)
            getpackagetype = sqltable.Rows(0)("ServiceName")
            sqlcon.Close()

            'get the tracking
            'get branch Name
            sqlconnect()
            sqltable = New DataTable
            sqladapter = New SqlClient.SqlDataAdapter("SELECT * FROM BRANCHES where ID = '" & Setbranchid & "'", sqlcon)
            sqladapter.Fill(sqltable)
            getbranchid = sqltable.Rows(0)("BranchName")
            Label15.Text = getbranchid & " Branch"
            sqlcon.Close()

            'get tracking status
            sqlconnect()
            sqltable = New DataTable
            sqladapter = New SqlClient.SqlDataAdapter("SELECT * FROM TRACKING WHERE  TrackingID='" & txttrackor.Text & "' and TrackingCode = '" & txttrackcode.Text & "'", sqlcon)
            sqladapter.Fill(sqltable)

            For Each tracks As DataRow In sqltable.Rows
                Me.Update()
                settrackstats = tracks.Item("StatusID")
                sqltable = New DataTable
                sqladapter = New SqlClient.SqlDataAdapter("SELECT * FROM TRACKINGSTATUS WHERE  ID = '" & settrackstats & "'", sqlcon)
                sqladapter.Fill(sqltable)
                ListBox1.Items.Add(sqltable.Rows(0)("TrackingStatus"))
            Next

            Label8.Text = airbill
            Label9.Text = branchcode
            Label10.Text = t_sender
            Label11.Text = receiver
            Label12.Text = getpackagetype
        Catch ex As Exception
            MsgBox(ex.Message)
            'MsgBox(ex.Message)
            Label8.Text = ""
            Label9.Text = ""
            Label10.Text = ""
            Label11.Text = ""
            Label12.Text = ""
            Label13.Text = ""
            Label15.Text = ""
            ListBox1.Items.Clear()
        End Try
    End Sub
End Class

1 个答案:

答案 0 :(得分:2)

救援时的连接池:

由于 connection pool 正在处理连接对象,因此在打开和关闭连接时不会出现性能问题。尼斯在文档中解释道:

  

为每个唯一连接字符串创建连接池。创建池时,会创建多个连接对象并将其添加到池中,以满足最小池大小要求。根据需要将连接添加到池中,直到指定的最大池大小(默认值为100)。当关闭或处置时,连接将被释放回池中。

单一连接:

如果我没有说服你并且你使用单一连接进行所有操作(在一种返回de连接的单例类中),你应该启用Multiple Active Result Sets (MARS)

  

多个活动结果集(MARS)是一种允许在单个连接上执行多个批处理的功能。在以前的版本中,一次只能对一个连接执行一个批处理。使用MARS执行多个批次并不意味着同时执行操作。

on 设置为<{3}}上的参数。

注意:您可能应该启用MARS,例如,您可以在同一连接上,在另一个查询的每次迭代中启动新查询。