I have a function call MyExecuteNonQuery(see bellow)
When I call it with some Sql command like:
MyExecuteNonQuery("Delete from myTable);
Will it close the conneciton which opened in MyExecuteNonQuery?







----------------------------------------------------------------------------
public int object MyExecuteNonQuery(string sSql)
{
string cnnStr="....";
SqlConnection cnn = new SqlConnection(cnnStr);
SqlCommand myCommand = new SqlCommand(sSql, cnn);
cnn.Open();
int iRV=myCommand.ExecuteNonQuery;
return iRV;

}
/*

Re: Wii close connection? by Marina

Marina
Tue Aug 30 15:39:20 CDT 2005

No, it will not. You are not closing the connection.

"ad" <flying@wfes.tcc.edu.tw> wrote in message
news:%23r1TpGarFHA.304@TK2MSFTNGP11.phx.gbl...
>I have a function call MyExecuteNonQuery(see bellow)
> When I call it with some Sql command like:
> MyExecuteNonQuery("Delete from myTable);
> Will it close the conneciton which opened in MyExecuteNonQuery?
>
>
>
>
>
>
>
> ----------------------------------------------------------------------------
> public int object MyExecuteNonQuery(string sSql)
> {
> string cnnStr="....";
> SqlConnection cnn = new SqlConnection(cnnStr);
> SqlCommand myCommand = new SqlCommand(sSql, cnn);
> cnn.Open();
> int iRV=myCommand.ExecuteNonQuery;
> return iRV;
>
> }
> /*
>



Re: Wii close connection? by Chris

Chris
Tue Aug 30 15:38:24 CDT 2005

Hi,

No, the connection will linger until such time as the garbage collection
process ie. Finalization etc. takes care of releasing the connection object.
You should explicitly close the connection, preferably by using a
try/finally block to ensure the connection is closed regardless of errors,
for C# I like the using statement to assist with this.

public int object MyExecuteNonQuery(string sSql)
{
string cnnStr="....";
int iRV;
using(SqlConnection cnn = new SqlConnection(cnnStr))
{
SqlCommand myCommand = new SqlCommand(sSql, cnn);
cnn.Open();
iRV=myCommand.ExecuteNonQuery;
} // At this point the cnn object will be disposed
return iRV;
}

Hope this helps

--
Chris Taylor
http://dotnetjunkies.com/weblog/chris.taylor


"ad" <flying@wfes.tcc.edu.tw> wrote in message
news:#r1TpGarFHA.304@TK2MSFTNGP11.phx.gbl...
> I have a function call MyExecuteNonQuery(see bellow)
> When I call it with some Sql command like:
> MyExecuteNonQuery("Delete from myTable);
> Will it close the conneciton which opened in MyExecuteNonQuery?
>
>
>
>
>
>
>
> --------------------------------------------------------------------------
--
> public int object MyExecuteNonQuery(string sSql)
> {
> string cnnStr="....";
> SqlConnection cnn = new SqlConnection(cnnStr);
> SqlCommand myCommand = new SqlCommand(sSql, cnn);
> cnn.Open();
> int iRV=myCommand.ExecuteNonQuery;
> return iRV;
>
> }
> /*
>
>



RE: Wii close connection? by MiloszSkalecki

MiloszSkalecki
Tue Aug 30 15:49:04 CDT 2005

Hi,

No, ExecuteNonQuery does not close the connection automatically because it
is designed for catalog operations. You can do many catalog operations using
one connection. Use try - catch - finally statement instead :

try
{
cnn.Open();
myCommand.ExecuteNonQuery();
//
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (cnn.State != ConnectionState.Closed)
cnn.Close();
}
--
Milosz Skalecki
MCP, MCAD


"ad" wrote:

> I have a function call MyExecuteNonQuery(see bellow)
> When I call it with some Sql command like:
> MyExecuteNonQuery("Delete from myTable);
> Will it close the conneciton which opened in MyExecuteNonQuery?
>
>
>
>
>
>
>
> ----------------------------------------------------------------------------
> public int object MyExecuteNonQuery(string sSql)
> {
> string cnnStr="....";
> SqlConnection cnn = new SqlConnection(cnnStr);
> SqlCommand myCommand = new SqlCommand(sSql, cnn);
> cnn.Open();
> int iRV=myCommand.ExecuteNonQuery;
> return iRV;
>
> }
> /*
>
>
>

Re: Wii close connection? by Scott

Scott
Tue Aug 30 15:49:24 CDT 2005

Please note that the Garbage Collection of the connection object in memory
and the closing of the Connection object (to the data source) are not the
same thing.

He should explicitly close the connection when he's done with it and then
the object can be destroyed based on memory management rules.

"Chris Taylor" <chris_taylor_za@hotmail.com> wrote in message
news:uvr5HOarFHA.3796@TK2MSFTNGP11.phx.gbl...
> Hi,
>
> No, the connection will linger until such time as the garbage collection
> process ie. Finalization etc. takes care of releasing the connection
> object.
> You should explicitly close the connection, preferably by using a
> try/finally block to ensure the connection is closed regardless of errors,
> for C# I like the using statement to assist with this.
>
> public int object MyExecuteNonQuery(string sSql)
> {
> string cnnStr="....";
> int iRV;
> using(SqlConnection cnn = new SqlConnection(cnnStr))
> {
> SqlCommand myCommand = new SqlCommand(sSql, cnn);
> cnn.Open();
> iRV=myCommand.ExecuteNonQuery;
> } // At this point the cnn object will be disposed
> return iRV;
> }
>
> Hope this helps
>
> --
> Chris Taylor
> http://dotnetjunkies.com/weblog/chris.taylor
>
>
> "ad" <flying@wfes.tcc.edu.tw> wrote in message
> news:#r1TpGarFHA.304@TK2MSFTNGP11.phx.gbl...
>> I have a function call MyExecuteNonQuery(see bellow)
>> When I call it with some Sql command like:
>> MyExecuteNonQuery("Delete from myTable);
>> Will it close the conneciton which opened in MyExecuteNonQuery?
>>
>>
>>
>>
>>
>>
>>
>> --------------------------------------------------------------------------
> --
>> public int object MyExecuteNonQuery(string sSql)
>> {
>> string cnnStr="....";
>> SqlConnection cnn = new SqlConnection(cnnStr);
>> SqlCommand myCommand = new SqlCommand(sSql, cnn);
>> cnn.Open();
>> int iRV=myCommand.ExecuteNonQuery;
>> return iRV;
>>
>> }
>> /*
>>
>>
>
>



RE: Wii close connection? by MiloszSkalecki

MiloszSkalecki
Tue Aug 30 15:55:02 CDT 2005

Or use "using" statement which is actually try-finally statement
using (SqlConnection oCOnnection = new SqlConnection)
{
// ........
}
--
Milosz Skalecki
MCP, MCAD


"ad" wrote:

> I have a function call MyExecuteNonQuery(see bellow)
> When I call it with some Sql command like:
> MyExecuteNonQuery("Delete from myTable);
> Will it close the conneciton which opened in MyExecuteNonQuery?
>
>
>
>
>
>
>
> ----------------------------------------------------------------------------
> public int object MyExecuteNonQuery(string sSql)
> {
> string cnnStr="....";
> SqlConnection cnn = new SqlConnection(cnnStr);
> SqlCommand myCommand = new SqlCommand(sSql, cnn);
> cnn.Open();
> int iRV=myCommand.ExecuteNonQuery;
> return iRV;
>
> }
> /*
>
>
>

Re: Wii close connection? by Chris

Chris
Tue Aug 30 16:36:33 CDT 2005

Yes, however once the GC process will eventually invoke the finalizer which
in turn calls Dispose and therefore the connection will be closed.

--
Chris Taylor
http://dotnetjunkies.com/weblog/chris.taylor


"Scott M." <s-mar@nospam.nospam> wrote in message
news:#isuiRarFHA.3640@tk2msftngp13.phx.gbl...
> Please note that the Garbage Collection of the connection object in memory
> and the closing of the Connection object (to the data source) are not the
> same thing.
>
> He should explicitly close the connection when he's done with it and then
> the object can be destroyed based on memory management rules.
>
> "Chris Taylor" <chris_taylor_za@hotmail.com> wrote in message
> news:uvr5HOarFHA.3796@TK2MSFTNGP11.phx.gbl...
> > Hi,
> >
> > No, the connection will linger until such time as the garbage collection
> > process ie. Finalization etc. takes care of releasing the connection
> > object.
> > You should explicitly close the connection, preferably by using a
> > try/finally block to ensure the connection is closed regardless of
errors,
> > for C# I like the using statement to assist with this.
> >
> > public int object MyExecuteNonQuery(string sSql)
> > {
> > string cnnStr="....";
> > int iRV;
> > using(SqlConnection cnn = new SqlConnection(cnnStr))
> > {
> > SqlCommand myCommand = new SqlCommand(sSql, cnn);
> > cnn.Open();
> > iRV=myCommand.ExecuteNonQuery;
> > } // At this point the cnn object will be disposed
> > return iRV;
> > }
> >
> > Hope this helps
> >
> > --
> > Chris Taylor
> > http://dotnetjunkies.com/weblog/chris.taylor
> >
> >
> > "ad" <flying@wfes.tcc.edu.tw> wrote in message
> > news:#r1TpGarFHA.304@TK2MSFTNGP11.phx.gbl...
> >> I have a function call MyExecuteNonQuery(see bellow)
> >> When I call it with some Sql command like:
> >> MyExecuteNonQuery("Delete from myTable);
> >> Will it close the conneciton which opened in MyExecuteNonQuery?
> >>
> >>
> >>
> >>
> >>
> >>
> >>
>
>> -------------------------------------------------------------------------
-
> > --
> >> public int object MyExecuteNonQuery(string sSql)
> >> {
> >> string cnnStr="....";
> >> SqlConnection cnn = new SqlConnection(cnnStr);
> >> SqlCommand myCommand = new SqlCommand(sSql, cnn);
> >> cnn.Open();
> >> int iRV=myCommand.ExecuteNonQuery;
> >> return iRV;
> >>
> >> }
> >> /*
> >>
> >>
> >
> >
>
>



Re: Wii close connection? by Scott

Scott
Tue Aug 30 17:50:01 CDT 2005

But, that changes when connection pooling (the default) is used. My point
was that it is confusing and not really the best answer to say that the GC
will take care of it.


"Chris Taylor" <chris_taylor_za@hotmail.com> wrote in message
news:OHwjnuarFHA.3864@TK2MSFTNGP10.phx.gbl...
> Yes, however once the GC process will eventually invoke the finalizer
> which
> in turn calls Dispose and therefore the connection will be closed.
>
> --
> Chris Taylor
> http://dotnetjunkies.com/weblog/chris.taylor
>
>
> "Scott M." <s-mar@nospam.nospam> wrote in message
> news:#isuiRarFHA.3640@tk2msftngp13.phx.gbl...
>> Please note that the Garbage Collection of the connection object in
>> memory
>> and the closing of the Connection object (to the data source) are not the
>> same thing.
>>
>> He should explicitly close the connection when he's done with it and then
>> the object can be destroyed based on memory management rules.
>>
>> "Chris Taylor" <chris_taylor_za@hotmail.com> wrote in message
>> news:uvr5HOarFHA.3796@TK2MSFTNGP11.phx.gbl...
>> > Hi,
>> >
>> > No, the connection will linger until such time as the garbage
>> > collection
>> > process ie. Finalization etc. takes care of releasing the connection
>> > object.
>> > You should explicitly close the connection, preferably by using a
>> > try/finally block to ensure the connection is closed regardless of
> errors,
>> > for C# I like the using statement to assist with this.
>> >
>> > public int object MyExecuteNonQuery(string sSql)
>> > {
>> > string cnnStr="....";
>> > int iRV;
>> > using(SqlConnection cnn = new SqlConnection(cnnStr))
>> > {
>> > SqlCommand myCommand = new SqlCommand(sSql, cnn);
>> > cnn.Open();
>> > iRV=myCommand.ExecuteNonQuery;
>> > } // At this point the cnn object will be disposed
>> > return iRV;
>> > }
>> >
>> > Hope this helps
>> >
>> > --
>> > Chris Taylor
>> > http://dotnetjunkies.com/weblog/chris.taylor
>> >
>> >
>> > "ad" <flying@wfes.tcc.edu.tw> wrote in message
>> > news:#r1TpGarFHA.304@TK2MSFTNGP11.phx.gbl...
>> >> I have a function call MyExecuteNonQuery(see bellow)
>> >> When I call it with some Sql command like:
>> >> MyExecuteNonQuery("Delete from myTable);
>> >> Will it close the conneciton which opened in MyExecuteNonQuery?
>> >>
>> >>
>> >>
>> >>
>> >>
>> >>
>> >>
>>
>>> -------------------------------------------------------------------------
> -
>> > --
>> >> public int object MyExecuteNonQuery(string sSql)
>> >> {
>> >> string cnnStr="....";
>> >> SqlConnection cnn = new SqlConnection(cnnStr);
>> >> SqlCommand myCommand = new SqlCommand(sSql, cnn);
>> >> cnn.Open();
>> >> int iRV=myCommand.ExecuteNonQuery;
>> >> return iRV;
>> >>
>> >> }
>> >> /*
>> >>
>> >>
>> >
>> >
>>
>>
>
>



Re: Wii close connection? by Chris

Chris
Wed Aug 31 11:11:36 CDT 2005

Is it not true that the GC will take care of it eventually? Of course it is,
I never stated that this is a good thing I am just stating that it will
happen.

As for the connection pooling issue, I believe this is beyond what the
poster was asking for.
But yes it is true that the underlying connection is not physically closed,
however the resource
is being released and being made available for reuse by other code within
the AppDomain.
This happens regardless of the managed connection being finalized or
explicitly calling Dispose or Close.

Again I state however:
"You should explicitly close the connection..." ie. call Close or Dispose on
the managed connection object.

Hope this clarifies any confusion my original response might have caused.

--
Chris Taylor
http://dotnetjunkies.com/weblog/chris.taylor


"Scott M." <s-mar@nospam.nospam> wrote in message
news:epAg8UbrFHA.1252@TK2MSFTNGP09.phx.gbl...
> But, that changes when connection pooling (the default) is used. My point
> was that it is confusing and not really the best answer to say that the GC
> will take care of it.
>
>
> "Chris Taylor" <chris_taylor_za@hotmail.com> wrote in message
> news:OHwjnuarFHA.3864@TK2MSFTNGP10.phx.gbl...
> > Yes, however once the GC process will eventually invoke the finalizer
> > which
> > in turn calls Dispose and therefore the connection will be closed.
> >
> > --
> > Chris Taylor
> > http://dotnetjunkies.com/weblog/chris.taylor
> >
> >
> > "Scott M." <s-mar@nospam.nospam> wrote in message
> > news:#isuiRarFHA.3640@tk2msftngp13.phx.gbl...
> >> Please note that the Garbage Collection of the connection object in
> >> memory
> >> and the closing of the Connection object (to the data source) are not
the
> >> same thing.
> >>
> >> He should explicitly close the connection when he's done with it and
then
> >> the object can be destroyed based on memory management rules.
> >>
> >> "Chris Taylor" <chris_taylor_za@hotmail.com> wrote in message
> >> news:uvr5HOarFHA.3796@TK2MSFTNGP11.phx.gbl...
> >> > Hi,
> >> >
> >> > No, the connection will linger until such time as the garbage
> >> > collection
> >> > process ie. Finalization etc. takes care of releasing the connection
> >> > object.
> >> > You should explicitly close the connection, preferably by using a
> >> > try/finally block to ensure the connection is closed regardless of
> > errors,
> >> > for C# I like the using statement to assist with this.
> >> >
> >> > public int object MyExecuteNonQuery(string sSql)
> >> > {
> >> > string cnnStr="....";
> >> > int iRV;
> >> > using(SqlConnection cnn = new SqlConnection(cnnStr))
> >> > {
> >> > SqlCommand myCommand = new SqlCommand(sSql, cnn);
> >> > cnn.Open();
> >> > iRV=myCommand.ExecuteNonQuery;
> >> > } // At this point the cnn object will be disposed
> >> > return iRV;
> >> > }
> >> >
> >> > Hope this helps
> >> >
> >> > --
> >> > Chris Taylor
> >> > http://dotnetjunkies.com/weblog/chris.taylor
> >> >
> >> >
> >> > "ad" <flying@wfes.tcc.edu.tw> wrote in message
> >> > news:#r1TpGarFHA.304@TK2MSFTNGP11.phx.gbl...
> >> >> I have a function call MyExecuteNonQuery(see bellow)
> >> >> When I call it with some Sql command like:
> >> >> MyExecuteNonQuery("Delete from myTable);
> >> >> Will it close the conneciton which opened in MyExecuteNonQuery?
> >> >>
> >> >>
> >> >>
> >> >>
> >> >>
> >> >>
> >> >>
> >>
>
>>> ------------------------------------------------------------------------
-
> > -
> >> > --
> >> >> public int object MyExecuteNonQuery(string sSql)
> >> >> {
> >> >> string cnnStr="....";
> >> >> SqlConnection cnn = new SqlConnection(cnnStr);
> >> >> SqlCommand myCommand = new SqlCommand(sSql, cnn);
> >> >> cnn.Open();
> >> >> int iRV=myCommand.ExecuteNonQuery;
> >> >> return iRV;
> >> >>
> >> >> }
> >> >> /*
> >> >>
> >> >>
> >> >
> >> >
> >>
> >>
> >
> >
>
>



Re: Wii close connection? by Scott

Scott
Wed Aug 31 11:53:00 CDT 2005

I found it, not so much confusing, as misleading to talk about the fact that
the connection will stay open until the GC gets its hands on the connection.
As we've discussed, connection pooling could keep the GC away from the
object indefinitely.



"Chris Taylor" <chris_taylor_za@hotmail.com> wrote in message
news:OE1budkrFHA.1984@tk2msftngp13.phx.gbl...
> Is it not true that the GC will take care of it eventually? Of course it
> is,
> I never stated that this is a good thing I am just stating that it will
> happen.
>
> As for the connection pooling issue, I believe this is beyond what the
> poster was asking for.
> But yes it is true that the underlying connection is not physically
> closed,
> however the resource
> is being released and being made available for reuse by other code within
> the AppDomain.
> This happens regardless of the managed connection being finalized or
> explicitly calling Dispose or Close.
>
> Again I state however:
> "You should explicitly close the connection..." ie. call Close or Dispose
> on
> the managed connection object.
>
> Hope this clarifies any confusion my original response might have caused.
>
> --
> Chris Taylor
> http://dotnetjunkies.com/weblog/chris.taylor
>
>
> "Scott M." <s-mar@nospam.nospam> wrote in message
> news:epAg8UbrFHA.1252@TK2MSFTNGP09.phx.gbl...
>> But, that changes when connection pooling (the default) is used. My
>> point
>> was that it is confusing and not really the best answer to say that the
>> GC
>> will take care of it.
>>
>>
>> "Chris Taylor" <chris_taylor_za@hotmail.com> wrote in message
>> news:OHwjnuarFHA.3864@TK2MSFTNGP10.phx.gbl...
>> > Yes, however once the GC process will eventually invoke the finalizer
>> > which
>> > in turn calls Dispose and therefore the connection will be closed.
>> >
>> > --
>> > Chris Taylor
>> > http://dotnetjunkies.com/weblog/chris.taylor
>> >
>> >
>> > "Scott M." <s-mar@nospam.nospam> wrote in message
>> > news:#isuiRarFHA.3640@tk2msftngp13.phx.gbl...
>> >> Please note that the Garbage Collection of the connection object in
>> >> memory
>> >> and the closing of the Connection object (to the data source) are not
> the
>> >> same thing.
>> >>
>> >> He should explicitly close the connection when he's done with it and
> then
>> >> the object can be destroyed based on memory management rules.
>> >>
>> >> "Chris Taylor" <chris_taylor_za@hotmail.com> wrote in message
>> >> news:uvr5HOarFHA.3796@TK2MSFTNGP11.phx.gbl...
>> >> > Hi,
>> >> >
>> >> > No, the connection will linger until such time as the garbage
>> >> > collection
>> >> > process ie. Finalization etc. takes care of releasing the connection
>> >> > object.
>> >> > You should explicitly close the connection, preferably by using a
>> >> > try/finally block to ensure the connection is closed regardless of
>> > errors,
>> >> > for C# I like the using statement to assist with this.
>> >> >
>> >> > public int object MyExecuteNonQuery(string sSql)
>> >> > {
>> >> > string cnnStr="....";
>> >> > int iRV;
>> >> > using(SqlConnection cnn = new SqlConnection(cnnStr))
>> >> > {
>> >> > SqlCommand myCommand = new SqlCommand(sSql, cnn);
>> >> > cnn.Open();
>> >> > iRV=myCommand.ExecuteNonQuery;
>> >> > } // At this point the cnn object will be disposed
>> >> > return iRV;
>> >> > }
>> >> >
>> >> > Hope this helps
>> >> >
>> >> > --
>> >> > Chris Taylor
>> >> > http://dotnetjunkies.com/weblog/chris.taylor
>> >> >
>> >> >
>> >> > "ad" <flying@wfes.tcc.edu.tw> wrote in message
>> >> > news:#r1TpGarFHA.304@TK2MSFTNGP11.phx.gbl...
>> >> >> I have a function call MyExecuteNonQuery(see bellow)
>> >> >> When I call it with some Sql command like:
>> >> >> MyExecuteNonQuery("Delete from myTable);
>> >> >> Will it close the conneciton which opened in MyExecuteNonQuery?
>> >> >>
>> >> >>
>> >> >>
>> >> >>
>> >> >>
>> >> >>
>> >> >>
>> >>
>>
>>>> ------------------------------------------------------------------------
> -
>> > -
>> >> > --
>> >> >> public int object MyExecuteNonQuery(string sSql)
>> >> >> {
>> >> >> string cnnStr="....";
>> >> >> SqlConnection cnn = new SqlConnection(cnnStr);
>> >> >> SqlCommand myCommand = new SqlCommand(sSql, cnn);
>> >> >> cnn.Open();
>> >> >> int iRV=myCommand.ExecuteNonQuery;
>> >> >> return iRV;
>> >> >>
>> >> >> }
>> >> >> /*
>> >> >>
>> >> >>
>> >> >
>> >> >
>> >>
>> >>
>> >
>> >
>>
>>
>
>