ASP.NET (VB) - Close an opened SQL connection inside Function
- by B1GB0Y
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 :)