In all of the web applications I write I make many calls to a SQL database, often in one postback. When I make these calls in multiple functions, they always start the same way
In forms where I'm calling multiple functions that do this, I'm wondering if I should have a SqlConnection that's global to the entire page so I'm not opening and closing so many SqlConnections. I would of course put it in my declarations, and then instantiate it in Page_Load. What are the pros and cons of this method over the method I'm currently using? Thanks, and have a great day.
Code:
Private Function MyFunction() As Boolean
Dim sSSQL As String = ""
Dim sqlConn As SqlConnection
Dim sqlCmd As SqlCommand
Dim sqlRdr As SqlDataReader
Try
...
sqlConn = New SqlConnection
sqlConn.ConnectionString = Session("ConnectionString")
sqlConn.Open()
...
Catch ex As Exception
Throw ex
Finally
If Not sqlRdr Is Nothing Then sqlRdr.Close()
If Not sqlConn Is Nothing Then sqlConn.Close()
End Try
End Function