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 8846251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:56:08+00:00 2026-06-14T11:56:08+00:00

Will the following statement cause a memory leak: Imports System.Data.SQLClient Public Function getConnection ()

  • 0

Will the following statement cause a memory leak:

Imports System.Data.SQLClient

Public Function getConnection () As SQLConnection
    return New SQLConnection()
End Function

Public Sub TestConnection()
    Dim con As SQLConnection
    con = getConnection
    con.close
    con = Nothing
End Sub

How does .close or .dispose get called on the SQLConnection in getConnection?

  • 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-14T11:56:09+00:00Added an answer on June 14, 2026 at 11:56 am

    There will be no memory leak because it will be garbage collected after you’ve called the method.

    But this method does nothing but causing confusion. You should always dispose connections(which closes it implicitely) as soon as you’re finished with it (even in case of an exception).

    You can do that in a finally of a Try/Finally or (easier) with the Using statement. But since both approaches need to wrap the connection, your method enables the calling method to forget it. Therefore it is bad practise.

    So simply do this:

    Public Sub TestConnection()
        Using con = New SqlConnection("connection string here")
            Using cmd = new SqlCommand("sql query here", con)
                ' do something, f.e. cmd.ExecuteNonQuery() '
            End Using
        End Using
    End Sub
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

From the mysql certification guide's example questions ... question Will the following SQL statement
select @result=@input.query('*') for xml raw,type Above statement will generate following alert: Msg 6819, Level
Will the following code cause a ConcurrentModificationException or other side effects? ArrayList<String> newList =
Will the following append() in the catch cause the rethrown exception to see the
Will the following program cause any problem during compiling and execution process? class A{
I have the following Execution statement which creates a table (using data from another
The following two statements will cause a compiler overflow error ( reason being that
After running the following SQL statements, you will see that, MySQL has automatically created
Will the following media queries behave exactly same on mobile devices and computers or
Will the following query evaluate to true (1), false (0), or NULL? SELECT '%'

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.