Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9272699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T15:55:59+00:00 2026-06-18T15:55:59+00:00

I am connecting to Access Database and I am wondering if overriding Sub finalize

  • 0

I am connecting to Access Database and I am wondering if overriding Sub finalize in my cconnectionDB.vb would be useful:

Imports System.Data.SqlClient
Imports System.Data.Common
Imports System.IO

Public Class DbConn
    Private DataCon As DbConnection
    Private DBConnectionOpen As Boolean = False

     Protected Overrides Sub finalize()
            Try
                DataCon.Close()
            Catch ex As Exception

            End Try
        End Sub

    Public Sub OpenDatabaseConnection()
        Try
            DataCon.Open()
        Catch ex As Exception
            Throw New System.Exception("Error opening data connection.", ex.InnerException)
            DBConnectionOpen = False
            Exit Sub
        End Try
        DBConnectionOpen = True
    End Sub 

    Public Sub CloseDatabaseConnection()
        DataCon.Close()
        DBConnectionOpen = False
    End Sub


   ''' <summary>
    ''' Creates a new connection to an Access database
    ''' </summary>
    ''' <param name="FileName">The full path of the Access file</param>
    ''' <remarks></remarks>
    Public Sub New(ByVal FileName As String)
        'Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\myFolder\myAccess2007file.accdb;Persist Security Info=False;
        Dim fileData As FileInfo = My.Computer.FileSystem.GetFileInfo(FileName)
        DataCon = New OleDb.OleDbConnection
        Dim csb As OleDb.OleDbConnectionStringBuilder = New OleDb.OleDbConnectionStringBuilder
        csb.ConnectionString = "Data Source=" & FileName
        Select Case fileData.Extension.ToLower
            Case ".mdb" : csb.Add("Provider", "Microsoft.Jet.Oledb.4.0")
            Case ".accdb" : csb.Add("Provider", "Microsoft.ACE.OLEDB.12.0")
        End Select
        DataCon.ConnectionString = csb.ConnectionString
        Try
            DataCon.Open()
            DataCon.Close()
        Catch ex As Exception
            Throw New System.Exception("Unable to connect to database.", ex.InnerException)
        End Try
    End Sub
End Class
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T15:56:00+00:00Added an answer on June 18, 2026 at 3:56 pm

    That’s not terribly useful. The Finalize deconstructor won’t be called until the garbage collector gets around to destroying the object. And, since the DbConn object would be the only thing that has a reference to the DbConnection object, it would automatically destroy that at the same time anyway. If you want to free the connection as soon as you are done with it, the recommended method is to implement the IDisposable interface in your class. For instance:

    Private Class DbConn
        Implements IDisposable
    
        Private DataCon As DbConnection
        Private disposedValue As Boolean
    
        Protected Overridable Sub Dispose(disposing As Boolean)
            If Not Me.disposedValue Then
                If disposing Then
                    Try
                        DataCon.Close()
                    Catch ex As Exception
                    End Try
                End If
            End If
            disposedValue = True
        End Sub
    
        Public Sub Dispose() Implements IDisposable.Dispose
            Dispose(True)
            GC.SuppressFinalize(Me)
        End Sub
    End Class
    

    However, if you implement IDisposable, your work isn’t done there. The Dispose method on IDisposable objects never gets called automatically. You need to tell the object when you are done with it by calling the Dispose method manually. Alternatively, you could use a Using block:

    Using d As New DbConn
        ' The DbConn.Dispose method automatically gets called once execution leaves this Using block
    End Using  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've recently made another question about connecting to MS-ACCESS database with .NET in C#
ms-access is connecting to a local mysql database the following code returns an error:
access is connecting to a mysql database. the rowsource of one of a listbox
I am using Access 2003 to access data stored in a SQL Server database
I'm connecting the access 2007 database from 5 different machines. (C#.Net) I'm having this
I am wondering how I could protect an Access database in a C# project
I am connecting a clients Access database to a website but need to add
I was wondering how could I retrieve the data from SQL server compact database
I am connecting to a Access DB, with following Conn String Conn.Open PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=\\server\share\Data\PFWTRAN.MDB
I want to gather some data from some tables of an Access Database, I've

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.