Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Do I Need To Close SQL Connection

Do I Need To Close SQL Connection

Scheduled Pinned Locked Moved Visual Basic
databasequestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    yccheok
    wrote on last edited by
    #1

    here is the portion of source code to retrieve data from database --------------------------- Dim dr as SQLDataReader Dim cmd as SQLCommand Dim cnn as SQLConnection cnn=new SQLConnection(ConfigurationSettings.AppSettings("ConnectionString")) cnn.open() cmd=new SQLCommand(sqlstr,cnn) dr=cmd.ExecuteReader() do while dr.read() hp=dr("handphone") loop dr.close() --------------------------- i was wondering do i need to close the SQLConnection cnn ?or when i close SQLDataReader dr, cnn will automatically be closed? thank you. regards yccheok

    N 1 Reply Last reply
    0
    • Y yccheok

      here is the portion of source code to retrieve data from database --------------------------- Dim dr as SQLDataReader Dim cmd as SQLCommand Dim cnn as SQLConnection cnn=new SQLConnection(ConfigurationSettings.AppSettings("ConnectionString")) cnn.open() cmd=new SQLCommand(sqlstr,cnn) dr=cmd.ExecuteReader() do while dr.read() hp=dr("handphone") loop dr.close() --------------------------- i was wondering do i need to close the SQLConnection cnn ?or when i close SQLDataReader dr, cnn will automatically be closed? thank you. regards yccheok

      N Offline
      N Offline
      Nick Seng
      wrote on last edited by
      #2

      You should close the connection only after you are done with the data reader. Fortunately, there is a way to automatically close the connection when you close the data reader, that is by supplying the following argument in the ExecuteReader Function:- cmd.ExecuteReader(CommandBehavior.CloseConnection) Notorious SMC


      The difference between the almost-right word & the right word is a really large matter - it's the difference between the lightning bug and the Lightning Mark Twain
      Get your facts first, and then you can distort them as much as you please Mark Twain

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups