PC Review
Forums
Newsgroups
Microsoft DotNet
Microsoft ADO .NET
Closing Connections
Forums
Newsgroups
Microsoft DotNet
Microsoft ADO .NET
Closing Connections
![]() |
Closing Connections |
|
|
Thread Tools | Rate Thread |
|
|
#1 |
|
Guest
Posts: n/a
|
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 |
|
|
|
#2 |
|
Guest
Posts: n/a
|
"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 > |
|
|
|
#3 |
|
Guest
Posts: n/a
|
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 > |
|
|
|
#4 |
|
Guest
Posts: n/a
|
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 >> > > |
|
|
|
#5 |
|
Guest
Posts: n/a
|
> 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. |
|
|
|
#6 |
|
Guest
Posts: n/a
|
"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 |
|
|
|
#7 |
|
Guest
Posts: n/a
|
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 > |
|
|
|
#8 |
|
Guest
Posts: n/a
|
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 >> > > |
|
|
|
#9 |
|
Guest
Posts: n/a
|
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 >>> >> >> > > |
|
|
|
#10 |
|
Guest
Posts: n/a
|
>
> "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 |
|
![]() |
|
| Thread Tools | |
| Rate This Thread | |
|
|

Main Page 

