VB.Net重用sql连接

时间:2013-02-05 16:42:58

标签: mysql vb.net

我是vb.net的新手,我想问一下是否有办法重用sql连接命令?

以下是我的main.vb的代码:

 Dim ServerString As String = "Server=localhost;User Id=root;Password=;Database=pos"
 Dim SQLConnection As MySqlConnection = New MySqlConnection

 Private Sub Main_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    SQLConnection.ConnectionString = ServerString

    Try
        If SQLConnection.State = ConnectionState.Closed Then
            SQLConnection.Open()
        Else
            SQLConnection.Close()
        End If
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try

由于我想在其他课程中使用此功能,因此我不想在每种形式中重写此代码。真的很感激任何帮助。感谢。

2 个答案:

答案 0 :(得分:2)

重用连接(或任何其他非托管资源)通常不是一个好主意。你应该尽快处理它们。

但是总是创建新连接没有问题,因为默认情况下你使用的是ADO.NET connection-pool。因此,您不会创建(并打开)新的物理连接。实际上,当你关闭/处理它时,你只是告诉游泳池可以在其他地方重复使用。当你打开它时,它不能在其他地方使用,这就是为什么始终关闭它很重要。

因此,请始终使用Using-statement

Public Shared Function GetColumn1(column2 As Int32) As String
    Dim sql = "SELECT Column1 From dbo.tableName WHERE Column2=@Column2 ORDER BY Column1 ASC"
    Using con = New SqlConnection(connectionString)
        Using cmd = New SqlCommand(sql, con)
            cmd.Parameters.AddWithValue("@Column2", column2)
            Try
                con.Open()
                Using rd = cmd.ExecuteReader()
                    If rd.Read() Then
                        Dim Column1 As String = rd.GetString(0)
                        Return Column1
                    Else
                        Return Nothing
                    End If
                End Using
            Catch ex As Exception
                ' log the exception here or do not catch it '
                ' note that you don't need a Finally to close the connection '
                ' since a Using-Statement disposes the object even in case of exception(which also closes a connection implicitely)
            End Try
        End Using
    End Using
End Function

上面是一个示例方法,要求您不要重复使用任何内容。

答案 1 :(得分:0)

这就是我通常做的事情:我创建了一个类,例如ConnectDB,以及此类中的方法,例如的getConnection。这是代码:

Imports System.Data
Imports System.Data.SqlClient

Public Class ConnectDB

    Public Shared Function GetConnection() As SqlConnection
        Dim dbConnString As String = "Data Source=(local);Initial Catalog=Northwind;Integrated Security=True"
        Return New SqlConnection(dbConnString)
    End Function
End Class


Then from the method that needs a connection to the database, I call this function.  Here is a sample code:

Imports System.Data.SqlClient

Public Class EmployeeDB

    Public Shared Function GetEmployees() As List(Of Employee)
        Dim con As SqlConnection = ConnectDB.GetConnection()
        Dim selectStmt As String = "SELECT * FROM Employees"
        Dim selectCmd As New SqlCommand(selectStmt, con)
        Dim employees As New List(Of Employee)

        Try
            con.Open()
            Dim reader As SqlDataReader = selectCmd.ExecuteReader()
            Do While reader.Read
                Dim employee as New Employee

                employee.LastName = reader("LastName").ToString
                employee.FirstName = reader("FirstName").ToString
                ...
                employees.Add(employee)
            Loop
            reader.Close()
         Catch ex As Exception
             Throw ex
         Finally
             con.Close()
         End Try
         Return employees
    End Function
End Class

您还可以修改selectStmt字符串以包含过滤条件,参数和排序顺序,就像上面的Tim示例一样,并为每个参数添加selectCmd.Parameters.AddWithValue("@<parameterName>", value)