Understanding CommandBehavior.CloseConnection

  • Thread starter Thread starter Michael
  • Start date Start date
M

Michael

I'm trying to understand CommandBehavior.CloseConnection, used in this
context

conn.Open()
cmd = New SqlCommand(sql, conn)
rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection)

Is that the exact same as if I had done this?

conn.Open()
cmd = New SqlCommand(sql, conn)
rdr = cmd.ExecuteReader()
conn.Close()

Thanks, and if there is anything else about this method that would be
helpful to know, I'd appreciate it!

--Michael
 
Hi Micheal,

Well, not exactly. In the first case the connection is closed only when rdr
is closed.
Otherwise it is the same.
 
CloseConnection simply means that when you call rdr.Close(), the connection
will be closed as well and you won't have to close it explicitly.
 
The reason this behavior was implemented is to permit you to pass a pointer
to the DataReader out of scope and still permit the associated connection to
be closed--even though the receiving scope cannot address the Connection
directly.

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
MVP, hRD
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
 
Back
Top