I've been trying to convert a database class to using the Using statement to help eliminate possible memory leaks. I'm a bit unsure about how to do this with datareaders since you can't close the connection and still read the reader. Can someone tell me if the following is correct? Since the return is within the using blocks, what happens to the connections and objects? Is there a better way?
Friend Function ExecuteDataReader(ByVal ConnString As String, ByVal SqlQuery As String, ByVal CmdType As System.Data.CommandType) As MySqlDataReader
Using SqlConn As New MySqlConnection(ConnString)
Using SqlCmd As New MySqlCommand(SqlQuery, SqlConn)
SqlCmd.CommandType = CmdType
Using drReader As MySqlDataReader = SqlCmd.ExecuteReader()
Catch ex As Exception
Throw New Exception("Error in MySqlDataBaseOperations.ExecuteDataReader with error: " & ex.ToString)