PC Review Forums Newsgroups Microsoft DotNet Microsoft ADO .NET Closing Connections

Reply

Closing Connections

 
Thread Tools Rate Thread
Old 23-06-2006, 12:59 AM   #1
tshad
Guest
 
Posts: n/a
Default Closing Connections


I am confused on whether a connection is open or closed.

I was told you didn't have to close a connection if you were binding to
DataGrids, DataReaders, DropDowns etc - that they would close them
themselves.

Is this the same for both the DataAdapters and DataReaders?

I was told the DataAdapters would leave the connection as it found it. If
the connection was open, it would leave it open and if was closed it would
close it.

What about if in the middle of binding to the DataGrid it got an error?
Would it close the connection or do you need to close it yourself?

Thanks,

Tom


  Reply With Quote
Old 23-06-2006, 02:44 AM   #2
Scott M.
Guest
 
Posts: n/a
Default Re: Closing Connections


"tshad" <tscheiderich@ftsolutions.com> wrote in message
news:%230tEefllGHA.1640@TK2MSFTNGP02.phx.gbl...
>I am confused on whether a connection is open or closed.
>
> I was told you didn't have to close a connection if you were binding to
> DataGrids, DataReaders, DropDowns etc - that they would close them
> themselves.


DataGrids, DataRepeaters, DropDowns, etc. have no knowledge of your database
whatsoever. They do not automatically close your connection. These
controls are bound to a DataSource, which is most likely a DataSet or
DataTable. In turn, the DataSet or DataTable are disconnected objects that,
themselves, have no direct relationship with your connection.

> Is this the same for both the DataAdapters and DataReaders?


DataAdapters and DataReaders are a different story, as they are connected
data objects. DataAdapters will automatically Open and Close their
underlying connections. DataReaders will only automatically close your
connection if you set up the DataReader to do so and only then if you Close
your DataReader.

>
> I was told the DataAdapters would leave the connection as it found it. If
> the connection was open, it would leave it open and if was closed it would
> close it.


Whoever is telling you these things is wrong and has very little
understanding of what these objects are and how they work.

> What about if in the middle of binding to the DataGrid it got an error?
> Would it close the connection or do you need to close it yourself?


This is why we have Try...Catch...End Try statements. With any database
operation the code that *could* fail should be in the Try and the close
calls should be in the Finally.

Try
'Do the connecting and binding here

Catch ex As Exception
'Deal with your exceptions as needed

Finally
'Explicitly close DataReaders and Connections here

End Try

Also, calling the Close() method of any ADO.NET object that exposes an
Open() method will NOT cause an exception if that object is already closed,
so you can't hurt yourself by calling close more than once. So, in short,
it is a good practice to call Close on anything that gets opened.


>
> Thanks,
>
> Tom
>



  Reply With Quote
Old 23-06-2006, 05:33 AM   #3
Cor Ligthert [MVP]
Guest
 
Posts: n/a
Default Re: Closing Connections

Tom,

I don't know where you got that information from opening and closing while
using Binding.

The dataadapter is a class that does a lot of handling. Including Open a
connection as that is not open and close it than again. If it is alreayd
open, it does not open it again, but keep in mind as well not close it.

For the rest do I know not other classes which do that (the tableadapter but
that is an inherited dataadapter).

Are you maybe confused with using "using" what is a command that opens and
disposes automaticly.
(Not in VB 2002/2003).

I hope this helps,

Cor

"tshad" <tscheiderich@ftsolutions.com> schreef in bericht
news:%230tEefllGHA.1640@TK2MSFTNGP02.phx.gbl...
>I am confused on whether a connection is open or closed.
>
> I was told you didn't have to close a connection if you were binding to
> DataGrids, DataReaders, DropDowns etc - that they would close them
> themselves.
>
> Is this the same for both the DataAdapters and DataReaders?
>
> I was told the DataAdapters would leave the connection as it found it. If
> the connection was open, it would leave it open and if was closed it would
> close it.
>
> What about if in the middle of binding to the DataGrid it got an error?
> Would it close the connection or do you need to close it yourself?
>
> Thanks,
>
> Tom
>



  Reply With Quote
Old 23-06-2006, 05:35 AM   #4
Cor Ligthert [MVP]
Guest
 
Posts: n/a
Default Re: Closing Connections

Scott,

I forgot to look at your message before I wrote mine,

Sorry

Cor


"Scott M." <s-mar@nospam.nospam> schreef in bericht
news:%23VEFrYmlGHA.1208@TK2MSFTNGP02.phx.gbl...
>
> "tshad" <tscheiderich@ftsolutions.com> wrote in message
> news:%230tEefllGHA.1640@TK2MSFTNGP02.phx.gbl...
>>I am confused on whether a connection is open or closed.
>>
>> I was told you didn't have to close a connection if you were binding to
>> DataGrids, DataReaders, DropDowns etc - that they would close them
>> themselves.

>
> DataGrids, DataRepeaters, DropDowns, etc. have no knowledge of your
> database whatsoever. They do not automatically close your connection.
> These controls are bound to a DataSource, which is most likely a DataSet
> or DataTable. In turn, the DataSet or DataTable are disconnected objects
> that, themselves, have no direct relationship with your connection.
>
>> Is this the same for both the DataAdapters and DataReaders?

>
> DataAdapters and DataReaders are a different story, as they are connected
> data objects. DataAdapters will automatically Open and Close their
> underlying connections. DataReaders will only automatically close your
> connection if you set up the DataReader to do so and only then if you
> Close your DataReader.
>
>>
>> I was told the DataAdapters would leave the connection as it found it.
>> If the connection was open, it would leave it open and if was closed it
>> would close it.

>
> Whoever is telling you these things is wrong and has very little
> understanding of what these objects are and how they work.
>
>> What about if in the middle of binding to the DataGrid it got an error?
>> Would it close the connection or do you need to close it yourself?

>
> This is why we have Try...Catch...End Try statements. With any database
> operation the code that *could* fail should be in the Try and the close
> calls should be in the Finally.
>
> Try
> 'Do the connecting and binding here
>
> Catch ex As Exception
> 'Deal with your exceptions as needed
>
> Finally
> 'Explicitly close DataReaders and Connections here
>
> End Try
>
> Also, calling the Close() method of any ADO.NET object that exposes an
> Open() method will NOT cause an exception if that object is already
> closed, so you can't hurt yourself by calling close more than once. So,
> in short, it is a good practice to call Close on anything that gets
> opened.
>
>
>>
>> Thanks,
>>
>> Tom
>>

>
>



  Reply With Quote
Old 23-06-2006, 10:03 PM   #5
Scott M.
Guest
 
Posts: n/a
Default Re: Closing Connections


> The dataadapter is a class that does a lot of handling. Including Open a
> connection as that is not open and close it than again. If it is alreayd
> open, it does not open it again, but keep in mind as well not close it.


Are you saying that a DataAdapter will open the conneciton but NOT close it?
If that is the case, I disagree. DataAdapters open AND close their
underlying connections.


  Reply With Quote
Old 24-06-2006, 05:55 AM   #6
Cor Ligthert [MVP]
Guest
 
Posts: n/a
Default Re: Closing Connections


"Scott M." <s-mar@nospam.nospam> schreef in bericht
news:OB1dsgwlGHA.3752@TK2MSFTNGP02.phx.gbl...
>
>> The dataadapter is a class that does a lot of handling. Including Open a
>> connection as that is not open and close it than again. If it is alreayd
>> open, it does not open it again, but keep in mind as well not close it.

>
> Are you saying that a DataAdapter will open the conneciton but NOT close
> it? If that is the case, I disagree. DataAdapters open AND close their
> underlying connections.

No if the dataadapter does not open the connection itself, than it does not
close the connection.

Cor


  Reply With Quote
Old 24-06-2006, 04:07 PM   #7
Scott M.
Guest
 
Posts: n/a
Default Re: Closing Connections

But it *does* open the connection and it *does* close the connection, Cor.


"Cor Ligthert [MVP]" <notmyfirstname@planet.nl> wrote in message
news:ug7S8o0lGHA.4076@TK2MSFTNGP03.phx.gbl...
>
> "Scott M." <s-mar@nospam.nospam> schreef in bericht
> news:OB1dsgwlGHA.3752@TK2MSFTNGP02.phx.gbl...
>>
>>> The dataadapter is a class that does a lot of handling. Including Open a
>>> connection as that is not open and close it than again. If it is alreayd
>>> open, it does not open it again, but keep in mind as well not close it.

>>
>> Are you saying that a DataAdapter will open the conneciton but NOT close
>> it? If that is the case, I disagree. DataAdapters open AND close their
>> underlying connections.

> No if the dataadapter does not open the connection itself, than it does
> not close the connection.
>
> Cor
>



  Reply With Quote
Old 24-06-2006, 04:50 PM   #8
Cor Ligthert [MVP]
Guest
 
Posts: n/a
Default Re: Closing Connections

Scott,

Did I write something else?

>>> The dataadapter is a class that does a lot of handling. Including Open a
>>> connection as that is not open and close it than again


Cor

"Scott M." <s-mar@nospam.nospam> schreef in bericht
news:eKmDW%235lGHA.3752@TK2MSFTNGP02.phx.gbl...
> But it *does* open the connection and it *does* close the connection, Cor.
>
>
> "Cor Ligthert [MVP]" <notmyfirstname@planet.nl> wrote in message
> news:ug7S8o0lGHA.4076@TK2MSFTNGP03.phx.gbl...
>>
>> "Scott M." <s-mar@nospam.nospam> schreef in bericht
>> news:OB1dsgwlGHA.3752@TK2MSFTNGP02.phx.gbl...
>>>
>>>> The dataadapter is a class that does a lot of handling. Including Open
>>>> a
>>>> connection as that is not open and close it than again. If it is
>>>> alreayd open, it does not open it again, but keep in mind as well not
>>>> close it.
>>>
>>> Are you saying that a DataAdapter will open the conneciton but NOT close
>>> it? If that is the case, I disagree. DataAdapters open AND close their
>>> underlying connections.

>> No if the dataadapter does not open the connection itself, than it does
>> not close the connection.
>>
>> Cor
>>

>
>



  Reply With Quote
Old 25-06-2006, 03:25 AM   #9
Scott M.
Guest
 
Posts: n/a
Default Re: Closing Connections

But then you wrote:

"If it is alreayd open, it does not open it again, but keep in mind as well
not close it."

And this is what I'm commenting on.


"Cor Ligthert [MVP]" <notmyfirstname@planet.nl> wrote in message
news:%23kP1rW6lGHA.2204@TK2MSFTNGP03.phx.gbl...
> Scott,
>
> Did I write something else?
>
>>>> The dataadapter is a class that does a lot of handling. Including Open
>>>> a
>>>> connection as that is not open and close it than again

>
> Cor
>
> "Scott M." <s-mar@nospam.nospam> schreef in bericht
> news:eKmDW%235lGHA.3752@TK2MSFTNGP02.phx.gbl...
>> But it *does* open the connection and it *does* close the connection,
>> Cor.
>>
>>
>> "Cor Ligthert [MVP]" <notmyfirstname@planet.nl> wrote in message
>> news:ug7S8o0lGHA.4076@TK2MSFTNGP03.phx.gbl...
>>>
>>> "Scott M." <s-mar@nospam.nospam> schreef in bericht
>>> news:OB1dsgwlGHA.3752@TK2MSFTNGP02.phx.gbl...
>>>>
>>>>> The dataadapter is a class that does a lot of handling. Including Open
>>>>> a
>>>>> connection as that is not open and close it than again. If it is
>>>>> alreayd open, it does not open it again, but keep in mind as well not
>>>>> close it.
>>>>
>>>> Are you saying that a DataAdapter will open the conneciton but NOT
>>>> close it? If that is the case, I disagree. DataAdapters open AND close
>>>> their underlying connections.
>>> No if the dataadapter does not open the connection itself, than it does
>>> not close the connection.
>>>
>>> Cor
>>>

>>
>>

>
>



  Reply With Quote
Old 25-06-2006, 06:12 AM   #10
Cor Ligthert [MVP]
Guest
 
Posts: n/a
Default Re: Closing Connections

>
> "If it is alreayd open, it does not open it again, but keep in mind as
> well not close it."
>
> And this is what I'm commenting on.
>

But that is as it acts. Otherwise you could never build a transaction
including more updates.

Cor


  Reply With Quote
Reply



Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off