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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T15:10:12+00:00 2026-06-01T15:10:12+00:00

Can anyone tell me how I close an opened SQL connection inside a Function?

  • 0

Can anyone tell me how I close an opened SQL connection inside a Function?

I call a Select Function like this:

   Function Selec(ByVal SQLStr As String) As SqlDataReader

        Dim SQLConn As New SqlConnection()
        Dim SQLCmd As New SqlCommand()

        SQLConn.ConnectionString = Session("bd")
        SQLConn.Open()

        SQLCmd.Connection = SQLConn
        SQLCmd.CommandText = SQLStr

        Selec = SQLCmd.ExecuteReader

    End Function

And in another page I do a While method to retrieve me the data like this:

(Note: BDcon.BD is the name of the Class that have Functions)

    Dim write as New BDcon.BD

    Dim menu As SqlDataReader = writeBD.Selec("SELECT something from Table")

While menu.Read

    'Do something

End While

    menu.Close 'This close just the DataReader and not the SqlConnection

Finally I want to Close my SQL Connection by Function like this:

    Function Close() As SqlConnection

        Dim SQLConn As New SqlConnection()
        SQLConn.ConnectionString = Session("bd")

        SQLConn.Close()

    End Function

I think that the problem is on the Close() Function, I want to close the connection but I don’t know how to call my Opened Conneciton.

  • 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-01T15:10:13+00:00Added an answer on June 1, 2026 at 3:10 pm

    i think its better to go for using consruct will do the task for you…

    using cn as new system.data.sqlclient.sqlconnection()
        cn.open
        '{do a bunch of other stuff with commands and datareaders here}
        cn.close 
    end using 
    

    or

    you can also make use of CommandBehavior Enumeration

    Read for this : ExecuteReader with CommanBehavior ( automatically close connection after reading data)

    CloseConnection – When the command is executed, the associated Connection object is closed when the associated DataReader object is closed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can anyone tell me what's wrong with this code? class Dataset < ActiveRecord::Base has_many
can anyone tell me what is the best way to do something like, from
Can anyone tell me why this c# console command not running on Java? I've
Can anyone tell me why this script, running on a fileset in automator leaves
Can anyone tell me how I can control the output from an SQL stored
Can anyone tell me How do I solve this error. Error: NullReferenceException was Unhandled
I've been look at this for the last hour or so. Can anyone tell
Can anyone tell me why Firefox would close a form early by adding a
Can anyone tell me whether Helvetica is a browser base font? If so, it
Can anyone tell me why ActionScript 3, a statically typed language, doesn't have generics?

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.