Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ASP.NET(VB)-关闭函数内打开的SQL连接_.net_Sql_Vb.net_Connection - Fatal编程技术网

ASP.NET(VB)-关闭函数内打开的SQL连接

ASP.NET(VB)-关闭函数内打开的SQL连接,.net,sql,vb.net,connection,.net,Sql,Vb.net,Connection,有人能告诉我如何关闭函数中打开的SQL连接吗 我调用如下选择函数: Function Selec(ByVal SQLStr As String) As SqlDataReader Dim SQLConn As New SqlConnection() Dim SQLCmd As New SqlCommand() SQLConn.ConnectionString = Session("bd") SQLConn.Open()

有人能告诉我如何关闭函数中打开的SQL连接吗

我调用如下选择函数:

   Function Selec(ByVal SQLStr As String) As SqlDataReader

        Dim SQLConn As New SqlConnection()
        Dim SQLCmd As New SqlCommand()

        SQLConn.ConnectionString = Session("bd")
        SQLConn.Open()

        SQLCmd.Connection = SQLConn
        SQLCmd.CommandText = SQLStr

        Selec = SQLCmd.ExecuteReader

    End Function
    Function Close() As SqlConnection

        Dim SQLConn As New SqlConnection()
        SQLConn.ConnectionString = Session("bd")

        SQLConn.Close()

    End Function
在另一个页面中,我使用While方法检索数据,如下所示:

   Function Selec(ByVal SQLStr As String) As SqlDataReader

        Dim SQLConn As New SqlConnection()
        Dim SQLCmd As New SqlCommand()

        SQLConn.ConnectionString = Session("bd")
        SQLConn.Open()

        SQLCmd.Connection = SQLConn
        SQLCmd.CommandText = SQLStr

        Selec = SQLCmd.ExecuteReader

    End Function
    Function Close() As SqlConnection

        Dim SQLConn As New SqlConnection()
        SQLConn.ConnectionString = Session("bd")

        SQLConn.Close()

    End Function
注意:BDcon.BD是具有函数的类的名称)

最后,我想通过如下函数关闭SQL连接:

   Function Selec(ByVal SQLStr As String) As SqlDataReader

        Dim SQLConn As New SqlConnection()
        Dim SQLCmd As New SqlCommand()

        SQLConn.ConnectionString = Session("bd")
        SQLConn.Open()

        SQLCmd.Connection = SQLConn
        SQLCmd.CommandText = SQLStr

        Selec = SQLCmd.ExecuteReader

    End Function
    Function Close() As SqlConnection

        Dim SQLConn As New SqlConnection()
        SQLConn.ConnectionString = Session("bd")

        SQLConn.Close()

    End Function

我想问题出在
Close()
函数上,我想关闭连接,但我不知道如何调用我打开的连接。

我想最好还是使用construct为您完成任务

using cn as new system.data.sqlclient.sqlconnection()
    cn.open
    '{do a bunch of other stuff with commands and datareaders here}
    cn.close 
end using 

您还可以使用
CommandBehavior枚举

请阅读以下内容:


CloseConnection—执行命令时,关联的DataReader对象关闭时,关联的连接对象关闭

我认为您应该更改函数以返回数据表并立即关闭连接。
例如:

Function Select(ByVal SQLStr As String) As DataTable
    Dim SQLConn As New SqlConnection(Session("bd"))
    Dim SQLCmd As New SqlCommand(SQLStr, SQLConn)
    Dim SQLAdapt As New SqlDataAdapter(SQLCmd)
    Dim SQLDt As New DataTable()
    SQLAdapt.Fill(SQLDt)
    SQLConn.Close()
    Return SQLDt
End Function

或者您可以使用函数
GetConnection
创建并返回一个SQLConnection,将其传递给
Select
函数(内部不创建新函数),然后手动关闭连接。

我认为您应该将SQLConnection类的对象作为前面创建的Close函数中的参数传递。
然后在该函数中使用该对象。

在原始代码中,无法关闭原始连接,因为连接对象的作用域仅限于创建它的函数(只能从创建它的函数内部访问)。我同意Marco的回答,您应该返回DataTable而不是SQLDataReader。这是一种稍微不同的方法,不需要SQLDataAdapter:

Function Selec(ByVal SQLStr As String) As DataTable
    Dim SQLConn As New SqlConnection(Session("bd"))
    Dim SQLCmd As New SqlCommand(SQLStr, SQLConn)
    Dim SQLDt As New DataTable()
    SQLConn.Open()
    SQLDt.Load(SQLCmd.ExecuteReader)
    'Close the connection as soon as it is no longer needed
    SQLConn.Close() 
    Return SQLDt
End Function
要使用返回的表,可以通过以下方式遍历数据行集合:

Sub DoSomthing()
    Dim menu As DataTable = Selec("SELECT * FROM SomeTable")
    For Each row As DataRow In menu.Rows
        ' do something
    Next
End Sub
另一种方法是在返回的DataTable上创建DataTableReader,尽管我不认为这样做有任何好处,只是它与原始代码更为相似:

Sub AlternateDoSomething()
    Dim dt As DataTable = Selec("SELECT * FROM SomeTable")
    Dim menu As DataTableReader = dt.CreateDataReader
    While menu.Read
        'Do something
    End While
    menu.Close()
End Sub

我已经用普拉内·拉纳的回答解决了我的问题

将CommandBehavior.CloseConnection添加到我的代码中

Selec功能的最终代码:

   Function Selec(ByVal SQLStr As String) As SqlDataReader

        Dim SQLConn As New SqlConnection()
        Dim SQLCmd As New SqlCommand()

        SQLConn.ConnectionString = Session("bd")
        SQLConn.Open()

        SQLCmd.Connection = SQLConn
        SQLCmd.CommandText = SQLStr

        Selec = SQLCmd.ExecuteReader(CommandBehavior.CloseConnection)

    End Function
再次感谢大家!:D