|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Closing ConnectionsI 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 "tshad" <tscheider***@ftsolutions.com> wrote in message DataGrids, DataRepeaters, DropDowns, etc. have no knowledge of your database 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. 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. > Whoever is telling you these things is wrong and has very little > 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. 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? This is why we have Try...Catch...End Try statements. With any database > Would it close the connection or do you need to close it yourself? 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. Show quote > > Thanks, > > Tom > Scott,
I forgot to look at your message before I wrote mine, Sorry Cor Show quote "Scott M." <s-mar@nospam.nospam> schreef in bericht news:%23VEFrYmlGHA.1208@TK2MSFTNGP02.phx.gbl... > > "tshad" <tscheider***@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 >> > > 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 Show quote "tshad" <tscheider***@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 > > The dataadapter is a class that does a lot of handling. Including Open a Are you saying that a DataAdapter will open the conneciton but NOT close it? > 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. If that is the case, I disagree. DataAdapters open AND close their underlying connections. "Scott M." <s-mar@nospam.nospam> schreef in bericht No if the dataadapter does not open the connection itself, than it does not 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. close the connection. Cor But it *does* open the connection and it *does* close the connection, Cor.
Show quote "Cor Ligthert [MVP]" <notmyfirstn***@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 > Scott,
Did I write something else? >>> The dataadapter is a class that does a lot of handling. Including Open a Cor>>> connection as that is not open and close it than again Show quote "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]" <notmyfirstn***@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 >> > > 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. Show quote "Cor Ligthert [MVP]" <notmyfirstn***@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]" <notmyfirstn***@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 >>> >> >> > > > But that is as it acts. Otherwise you could never build a transaction > "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. > including more updates. Cor Cor, a DataAdapter will always close the connection it is configured to use
after it has executed its appropriate commands. It does not matter if the connection was already open prior to the DataAdapter's code executing. http://davidhayden.com/blog/dave/archive/2005/11/04/2555.aspx Show quote "Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> wrote in message news:eLNTpWBmGHA.3352@TK2MSFTNGP02.phx.gbl... > > >> "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 > Scott,
It is as is written on the by you showed page as. Thus, the SqlDataAdapter always leaves the connection in the same state it took it as. Pro ADO.NET 2.0 (Page 190). So if it is already open, it gives it back open and will not close it. That is exactly as the code that Miha made from the text that I had written. (Although I know that Miha knows this, he has as well stated this often in this newsgroup). Cor Show quote "Scott M." <NoSpam@NoSpam.com> schreef in bericht news:%23$QPFDLmGHA.4064@TK2MSFTNGP02.phx.gbl... > Cor, a DataAdapter will always close the connection it is configured to > use after it has executed its appropriate commands. It does not matter if > the connection was already open prior to the DataAdapter's code executing. > > http://davidhayden.com/blog/dave/archive/2005/11/04/2555.aspx > > > > "Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> wrote in message > news:eLNTpWBmGHA.3352@TK2MSFTNGP02.phx.gbl... >> > >>> "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 >> > > No, no, Cor is right. But hey, why take our word - try it for yourself.
-- Show quoteMiha Markic [MVP C#, INETA Country Leader for Slovenia] RightHand .NET consulting & development www.rthand.com Blog: http://cs.rthand.com/blogs/blog_with_righthand/ "Scott M." <NoSpam@NoSpam.com> wrote in message news:%23$QPFDLmGHA.4064@TK2MSFTNGP02.phx.gbl... > Cor, a DataAdapter will always close the connection it is configured to > use after it has executed its appropriate commands. It does not matter if > the connection was already open prior to the DataAdapter's code executing. > > http://davidhayden.com/blog/dave/archive/2005/11/04/2555.aspx > > > > "Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> wrote in message > news:eLNTpWBmGHA.3352@TK2MSFTNGP02.phx.gbl... >> > >>> "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 >> > > I stand corrected. My apologies, Cor. There is a lot of incorrect
documentation out there. -Scott Show quote "Miha Markic [MVP C#]" <miha at rthand com> wrote in message news:OK5kIPPmGHA.3980@TK2MSFTNGP02.phx.gbl... > No, no, Cor is right. But hey, why take our word - try it for yourself. > > -- > Miha Markic [MVP C#, INETA Country Leader for Slovenia] > RightHand .NET consulting & development www.rthand.com > Blog: http://cs.rthand.com/blogs/blog_with_righthand/ > > "Scott M." <NoSpam@NoSpam.com> wrote in message > news:%23$QPFDLmGHA.4064@TK2MSFTNGP02.phx.gbl... >> Cor, a DataAdapter will always close the connection it is configured to >> use after it has executed its appropriate commands. It does not matter >> if the connection was already open prior to the DataAdapter's code >> executing. >> >> http://davidhayden.com/blog/dave/archive/2005/11/04/2555.aspx >> >> >> >> "Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> wrote in message >> news:eLNTpWBmGHA.3352@TK2MSFTNGP02.phx.gbl... >>> > >>>> "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 >>> >> >> > > Hi Scott,
I think Cor's english is not perfect and that's why you misunderstood him. I think he is saying the same, you just need Cor's to English dictionary ;-) -- Show quoteMiha Markic [MVP C#, INETA Country Leader for Slovenia] RightHand .NET consulting & development www.rthand.com Blog: http://cs.rthand.com/blogs/blog_with_righthand/ "Scott M." <s-mar@nospam.nospam> wrote in message news:egn%23Q5$lGHA.464@TK2MSFTNGP05.phx.gbl... > 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. Miha,
No probably we are not saying the same. > But it *does* open the connection and it *does* close the connection, Cor This can mean: it does *forever* open the connection and it does *forever* close the connection, Cor However you interrupted it now in a way which had nothing to do with the discussion and did add absolute nothing to that. If you think that my sentence is in English not perfect (beside the obvious typo). Than please tell what it has to be. Cor Show quote "Miha Markic [MVP C#]" <miha at rthand com> schreef in bericht news:%23cdxRZCmGHA.492@TK2MSFTNGP05.phx.gbl... > Hi Scott, > > I think Cor's english is not perfect and that's why you misunderstood him. > I think he is saying the same, you just need Cor's to English dictionary > ;-) > > -- > Miha Markic [MVP C#, INETA Country Leader for Slovenia] > RightHand .NET consulting & development www.rthand.com > Blog: http://cs.rthand.com/blogs/blog_with_righthand/ > > > "Scott M." <s-mar@nospam.nospam> wrote in message > news:egn%23Q5$lGHA.464@TK2MSFTNGP05.phx.gbl... >> 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. > > bool isConnectionOpened = "is connection opened";
if (!isConnectionOpened) Connection.Open(); try { Fill(...)... // actuall fill invocation } finally { if (!isConnectionOpen) Connection.Close(); } Here, un universal language of what Fill method does regarding the connection ;-) -- Show quoteMiha Markic [MVP C#, INETA Country Leader for Slovenia] RightHand .NET consulting & development www.rthand.com Blog: http://cs.rthand.com/blogs/blog_with_righthand/ "Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> wrote in message news:ujFFk5DmGHA.508@TK2MSFTNGP03.phx.gbl... > Miha, > > No probably we are not saying the same. > >> But it *does* open the connection and it *does* close the connection, Cor > This can mean: it does *forever* open the connection and it does *forever* > close the connection, Cor > > However you interrupted it now in a way which had nothing to do with the > discussion and did add absolute nothing to that. If you think that my > sentence is in English not perfect (beside the obvious typo). Than please > tell what it has to be. > > Cor > > "Miha Markic [MVP C#]" <miha at rthand com> schreef in bericht > news:%23cdxRZCmGHA.492@TK2MSFTNGP05.phx.gbl... >> Hi Scott, >> >> I think Cor's english is not perfect and that's why you misunderstood >> him. >> I think he is saying the same, you just need Cor's to English dictionary >> ;-) >> >> -- >> Miha Markic [MVP C#, INETA Country Leader for Slovenia] >> RightHand .NET consulting & development www.rthand.com >> Blog: http://cs.rthand.com/blogs/blog_with_righthand/ >> >> >> "Scott M." <s-mar@nospam.nospam> wrote in message >> news:egn%23Q5$lGHA.464@TK2MSFTNGP05.phx.gbl... >>> 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. >> >> > > |
|||||||||||||||||||||||