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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:04:43+00:00 2026-06-16T03:04:43+00:00

I have been using SQLDataReader for fetchig some data from a db. Once I

  • 0

I have been using SQLDataReader for fetchig some data from a db. Once I used Reader with the the connection I am closing the connection only and not the Reader. Do we have any possibility of connection leak

Here is the code that I am using

 Public Sub Get_SomeData(ByVal sCon As String,ByRef ObjectToReturn As SomeClass)
        Dim sqlCon As SqlConnection = New SqlConnection(sCon)
        Dim sqlR As SqlDataReader = Nothing
        Dim sqlCmd As SqlCommand = New SqlCommand
        sqlCmd.CommandType = CommandType.StoredProcedure
        sqlCmd.Connection = sqlCon
        sqlCmd.CommandText = "get_SomeData"
        sqlCon.Open()
        sqlR = sqlCmd.ExecuteReader(CommandBehavior.CloseConnection)
        If sqlR.HasRows And sqlR.Read Then
            ObjectToReturn.Property1 = sqlR("Column1").ToString
            ObjectToReturn.Property1 = sqlR("Column1").ToString
            ObjectToReturn.Property1 = sqlR("Column1").ToString
            ObjectToReturn.Property1 = sqlR("Column1").ToString
        End If
        sqlCon.Close()
    End Sub
  • 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-16T03:04:45+00:00Added an answer on June 16, 2026 at 3:04 am

    No, closing the connection will suffice, but a better approach is through the Using statement

        Using sqlCon = New SqlConnection(sCon)
            Dim sqlR As SqlDataReader = Nothing
            Using sqlCmd = New SqlCommand
                sqlCmd.CommandType = CommandType.StoredProcedure
                sqlCmd.Connection = sqlCon
                sqlCmd.CommandText = "get_SomeData"
                sqlCon.Open()
                Using sqlR = sqlCmd.ExecuteReader()
                    If sqlR.HasRows And sqlR.Read Then
                        ObjectToReturn.Property1 = sqlR("Column1").ToString
                        .......
                   End If
                End Using
           End Using
      End Using
    

    The important part in MSDN docs about Using

    Sometimes your code requires an unmanaged resource, such as a file
    handle, a COM wrapper, or a SQL connection. A Using block guarantees
    the disposal of one or more such resources when your code is finished
    with them. This makes them available for other code to use.

    Managed resources are disposed of by the .NET Framework garbage
    collector (GC) without any extra coding on your part. You do not need
    a Using block for managed resources. However, you can still use a
    Using block to force the disposal of a managed resource instead of
    waiting for the garbage collector.

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

Sidebar

Related Questions

I have been using some Javascript to create a text field once an certain
I have been using TortoiseSVN for some time and I really like it. I
I have been using an API to do some work. This is how I
I have been using Matlab 2011b and contourf/contourfm to plot 2D data on a
I have been using echo $.sprintf(%01\$.2f,$numvar); for my USD formatting, but I only copied
Have been using a simple CSS only tooltip. Working Example css: .tip { position:relative;
I have been experimenting with ways to read data from a SQL server as
Have been using the SharpeRatio.annualized() function from Performance Analytics for awhile with no problems.
I have been using a reset CSS set of styles for quite some time
I have been using UNIX for years and the dcop command only recently came

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.