473,398 Members | 2,389 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

ODBC Query question

Is there anything wrong with immediately disposing of an OdbcCommand object
after calling its ExecuteReader method?
Yes, this example runs without error, but I'm concerned about a possible
timing issue in which garbage collection might destroy the OdbCommand object
when in fact it is still being referenced by the OdbcDataReader object (if
that's the case).

For example:

public static OdbcDataReader ExecuteReader(OdbcConnection aConnection,
string sSQL, UlSimpleMsgDelegate errorCallBack)
{
OdbcDataReader result;

try
{
OdbcCommand cmd = aConnection.CreateCommand();
try
{
cmd.CommandText = sSQL;
result = cmd.ExecuteReader();
}
finally
{
cmd.Dispose();
cmd = null;
}
}
catch(Exception e)
{
if (errorCallBack != null)
{
errorCallBack("Error Message : " + e.Message);
errorCallBack("SQL : " + sSQL);
errorCallBack("Stack Trace: " + e.StackTrace);
}
throw e;
}

return result;
}
Thanks,
Dave
Nov 16 '05 #1
3 3168
Dave,

I don't believe so. When you call Dispose on the command, it should not
dispose of the underlying connection. Once you obtain the reader, there is
little use for the command anymore, since the reader is just dealing with
streaming the result.

The only thing that I would be worried about is the connection state of
the connection, making sure it is closed properly.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"D. Yates" <fo****@hotmail.com> wrote in message
news:OV**************@TK2MSFTNGP14.phx.gbl...
Is there anything wrong with immediately disposing of an OdbcCommand
object
after calling its ExecuteReader method?
Yes, this example runs without error, but I'm concerned about a possible
timing issue in which garbage collection might destroy the OdbCommand
object
when in fact it is still being referenced by the OdbcDataReader object (if
that's the case).

For example:

public static OdbcDataReader ExecuteReader(OdbcConnection
aConnection,
string sSQL, UlSimpleMsgDelegate errorCallBack)
{
OdbcDataReader result;

try
{
OdbcCommand cmd = aConnection.CreateCommand();
try
{
cmd.CommandText = sSQL;
result = cmd.ExecuteReader();
}
finally
{
cmd.Dispose();
cmd = null;
}
}
catch(Exception e)
{
if (errorCallBack != null)
{
errorCallBack("Error Message : " + e.Message);
errorCallBack("SQL : " + sSQL);
errorCallBack("Stack Trace: " + e.StackTrace);
}
throw e;
}

return result;
}
Thanks,
Dave

Nov 16 '05 #2
Thanks for your replay, Nicholas.

Yes, we do dispose of the OdbcConnection properly before exiting the
application.

We were just looking to create a generic ExecuteReader method that would log
any errors, the stack trace and the SQL that caused the problem without
repeating the same code all over the place.

Thanks again.

Dave
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:OG**************@TK2MSFTNGP11.phx.gbl...
Dave,

I don't believe so. When you call Dispose on the command, it should not dispose of the underlying connection. Once you obtain the reader, there is little use for the command anymore, since the reader is just dealing with
streaming the result.

The only thing that I would be worried about is the connection state of the connection, making sure it is closed properly.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"D. Yates" <fo****@hotmail.com> wrote in message
news:OV**************@TK2MSFTNGP14.phx.gbl...
Is there anything wrong with immediately disposing of an OdbcCommand
object
after calling its ExecuteReader method?
Yes, this example runs without error, but I'm concerned about a possible
timing issue in which garbage collection might destroy the OdbCommand
object
when in fact it is still being referenced by the OdbcDataReader object (if that's the case).

For example:

public static OdbcDataReader ExecuteReader(OdbcConnection
aConnection,
string sSQL, UlSimpleMsgDelegate errorCallBack)
{
OdbcDataReader result;

try
{
OdbcCommand cmd = aConnection.CreateCommand();
try
{
cmd.CommandText = sSQL;
result = cmd.ExecuteReader();
}
finally
{
cmd.Dispose();
cmd = null;
}
}
catch(Exception e)
{
if (errorCallBack != null)
{
errorCallBack("Error Message : " + e.Message);
errorCallBack("SQL : " + sSQL);
errorCallBack("Stack Trace: " + e.StackTrace);
}
throw e;
}

return result;
}
Thanks,
Dave


Nov 16 '05 #3
Dave,

That should be fine, but I would favor the use of the "using" statement
over generating your own try/finally clause.

You can replace:

OdbcCommand cmd = aConnection.CreateCommand();
try
{
cmd.CommandText = sSQL;
result = cmd.ExecuteReader();
}
finally
{
cmd.Dispose();
cmd = null;
}

With:

using (OdbcCommand cmd = aConnection.CreateCommand())
{
cmd.CommandText = sSQL;
result = cmd.ExecuteReader();
}

The second looks MUCH better to me.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"D. Yates" <fo****@hotmail.com> wrote in message
news:uF*************@TK2MSFTNGP12.phx.gbl...
Thanks for your replay, Nicholas.

Yes, we do dispose of the OdbcConnection properly before exiting the
application.

We were just looking to create a generic ExecuteReader method that would
log
any errors, the stack trace and the SQL that caused the problem without
repeating the same code all over the place.

Thanks again.

Dave
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in
message news:OG**************@TK2MSFTNGP11.phx.gbl...
Dave,

I don't believe so. When you call Dispose on the command, it should

not
dispose of the underlying connection. Once you obtain the reader, there

is
little use for the command anymore, since the reader is just dealing with
streaming the result.

The only thing that I would be worried about is the connection state

of
the connection, making sure it is closed properly.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"D. Yates" <fo****@hotmail.com> wrote in message
news:OV**************@TK2MSFTNGP14.phx.gbl...
> Is there anything wrong with immediately disposing of an OdbcCommand
> object
> after calling its ExecuteReader method?
> Yes, this example runs without error, but I'm concerned about a
> possible
> timing issue in which garbage collection might destroy the OdbCommand
> object
> when in fact it is still being referenced by the OdbcDataReader object (if > that's the case).
>
> For example:
>
> public static OdbcDataReader ExecuteReader(OdbcConnection
> aConnection,
> string sSQL, UlSimpleMsgDelegate errorCallBack)
> {
> OdbcDataReader result;
>
> try
> {
> OdbcCommand cmd = aConnection.CreateCommand();
> try
> {
> cmd.CommandText = sSQL;
> result = cmd.ExecuteReader();
> }
> finally
> {
> cmd.Dispose();
> cmd = null;
> }
> }
> catch(Exception e)
> {
> if (errorCallBack != null)
> {
> errorCallBack("Error Message : " + e.Message);
> errorCallBack("SQL : " + sSQL);
> errorCallBack("Stack Trace: " + e.StackTrace);
> }
> throw e;
> }
>
> return result;
> }
>
>
> Thanks,
> Dave
>
>



Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Steve | last post by:
I normally use MySQL with PHP, but I'm delving into connecting to Access with ODBC (for a database that I already have set up at work), and I'm running into a couple of errors. I'm just trying to...
0
by: Sean Anderson | last post by:
ODBC under System DSN Setup Access Driver give it the DSN (Data Source Name) MSA Click on Select and point to the myfile.mdb (your database file)
13
by: Graham | last post by:
I need a SQL Server or ODBC package for Python. Can anyone help please? Have search the Python packages under Database and there is no reference to either SQL Server or ODBC. Thanks Graham
6
by: Andreas Lauffer | last post by:
I changed from Access97 to AccessXP and I have immense performance problems. Details: - Access XP MDB with Jet 4.0 ( no ADP-Project ) - Linked Tables to SQL-Server 2000 over ODBC I used...
1
by: Tim | last post by:
Hi all, Here is a brief description of a problem I encountered, and how I found a work around after 3 long days. I have a VB6 app that uses ADO and ODBC to get communicate with SQL server 2000...
8
by: Alfonso Esteban Gonzalez Sencion | last post by:
I am trying to use Access as a front end for extracting information from an Oracle database. I started using linked tables but I am getting a very curious behaviour. When I consult the linked...
8
by: Greg Strong | last post by:
Hello All, The short questions are 1 Do you know how to make DSN connection close in Access to Oracle 10g Express Edition? &/or 2 Do you know how to make a DSN-less pass-through query...
9
by: Greg Strong | last post by:
Hello All, What is the maximum length of an ODBC pass through query? Things work fine with the code except when I try to create a view which is pretty complex in Oracle. I'm using a DSN...
11
by: funky | last post by:
hello, I've got a big problem ad i'm not able to resolve it. We have a server running oracle 10g version 10.1.0. We usually use access as front end and connect database tables for data extraction....
3
by: wjreichard | last post by:
OK ... I am using UPS Worldship that issues an ODBC query to my MS2K server ... Worldship can query either a table or a view and retreive shipping info for a supplied orderid. I need to create a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.