T
tascien
I am getting confused about the role of any of the above methods and
interfaced. But what I want to do is simple... the Destructor such as
the one in C#.
Basically, my only issue, is to make sure the Data Base connection
inside my class, DBCONN is closed, when the object is out of scope or
destroyed in either way.
In dotnet 1.1, the code below used to work. As the page finished
loading, or when db=Nothing, it should have written out:
Connection started
Connection Killed
but in dotnet 2.0, it doesn't. am I missing something here? I read the
information at:
http://msdn2.microsoft.com/en-us/library/b1yfkh5e.aspx
but I do not want to completely take away the system's responsibility
to clean up. I just want to make sure the connection is closed. the
system should still have the responsibility to clean up.
In other words, I want a method that runs when the class is finished
or out of scope.
' ASPX code
--------------------------------------------
dim db = new DBCONN();
db = Nothing
' CLASS CODE
----------------------------------------------
Imports System.Data.SqlClient
Public Class DBCONN
Implements IDisposable
Private Conn As SqlConnection = Nothing
Sub New()
HttpContext.Current.Response.Write("Connection started")
Conn = New SqlConnection("ConnStr")
End Sub
Public Sub OpenConnection()
If Conn.State <> ConnectionState.Open Then
Conn.Open()
End If
End Sub
Public Sub CloseConnection()
If Conn.State <> ConnectionState.Closed Then
Conn.Close()
End If
End Sub
Sub Dispose() Implements System.IDisposable.Dispose
If Not Me.Conn Is Nothing Then
Me.CloseConnection()
Me.Conn = Nothing
End If
Web.HttpContext.Current.Response.Write("Connection Killed")
End Sub
End Class
interfaced. But what I want to do is simple... the Destructor such as
the one in C#.
Basically, my only issue, is to make sure the Data Base connection
inside my class, DBCONN is closed, when the object is out of scope or
destroyed in either way.
In dotnet 1.1, the code below used to work. As the page finished
loading, or when db=Nothing, it should have written out:
Connection started
Connection Killed
but in dotnet 2.0, it doesn't. am I missing something here? I read the
information at:
http://msdn2.microsoft.com/en-us/library/b1yfkh5e.aspx
but I do not want to completely take away the system's responsibility
to clean up. I just want to make sure the connection is closed. the
system should still have the responsibility to clean up.
In other words, I want a method that runs when the class is finished
or out of scope.
' ASPX code
--------------------------------------------
dim db = new DBCONN();
db = Nothing
' CLASS CODE
----------------------------------------------
Imports System.Data.SqlClient
Public Class DBCONN
Implements IDisposable
Private Conn As SqlConnection = Nothing
Sub New()
HttpContext.Current.Response.Write("Connection started")
Conn = New SqlConnection("ConnStr")
End Sub
Public Sub OpenConnection()
If Conn.State <> ConnectionState.Open Then
Conn.Open()
End If
End Sub
Public Sub CloseConnection()
If Conn.State <> ConnectionState.Closed Then
Conn.Close()
End If
End Sub
Sub Dispose() Implements System.IDisposable.Dispose
If Not Me.Conn Is Nothing Then
Me.CloseConnection()
Me.Conn = Nothing
End If
Web.HttpContext.Current.Response.Write("Connection Killed")
End Sub
End Class