ASP.NET (VB) - Close an opened SQL connection inside Function

Posted by B1GB0Y on Stack Overflow See other posts from Stack Overflow or by B1GB0Y
Published on 2012-04-11T11:16:04Z Indexed on 2012/04/11 11:29 UTC
Read the original article Hit count: 289

Filed under:
|
|
|
|

Can anyone tell me how I close an opened SQL connection inside a Function?

I call a Select Function like this:

   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

And in another page I do a While method to retrieve me the data like this:

(Note: BDcon.BD is the name of the Class that have Functions)

    Dim write as New BDcon.BD

    Dim menu As SqlDataReader = writeBD.Selec("SELECT something from Table")

While menu.Read

    'Do something

End While

    menu.Close 'This close just the DataReader and not the SqlConnection

Finally I want to Close my SQL Connection by Function like this:

    Function Close() As SqlConnection

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

        SQLConn.Close()

    End Function

I think that the problem is on the Close() Function, I want to close the connection but I don't know how to call my Opened Conneciton.

Please anyone can help me? Thanks in advance :)

© Stack Overflow or respective owner

Related posts about sql

Related posts about function