Hello everyone,
Thanks again for your help. The reason why I asked this
question is because I am having doubts on what I did to
my code. You see, I have a code that looks like this:
public bool getFromTableENROLLEE_TA(String OldGSISNo, out
OdbcDataReader ETAReader)
{
string slqSelect = "select .. from <table>";
ETAReader = null;
using (OdbcConnection cnn = new OdbcConnection(@DB2ConnectionStr))
{
using (OdbcCommand cmd = new OdbcCommand())
{
cmd.Connection = cnn;
cmd.CommandText = sqlSelect;
cmd.CommandType = CommandType.Text;
OdbcDataReader reader = null;
try
{
cnn.Open();
reader = cmd.ExecuteReader();
if (reader.Read())
{
ETAReader = reader;
return;
}
...
}
}
}
You will notice that I passed in a reader object as a parameter.
What concerns me is the state of the 3 objects: Connection, Command
and the reader objects. If I managed to get the values off from the
reader, how am I sure that the connection and command objects
were disposed of properly? Should I dispose them from the caller?
Is this considered a bad practice?
If this code needs revising I will do it, with your help of course. :)
Regards,
Benj
On Sep 25, 2:16 am, Joe Enos <[EMAIL PROTECTED]> wrote:
> I'm with rhaazy on this, except with one addition - make sure your
> connection is still open when you pass it out. For example:
>
> public void DoSomethingWithReader(string sql)
> {
> SqlDataReader reader = null;
> using (SqlConnection conn = {...})
> {
> using (SqlCommand comm = {...})
> {
> reader = comm.ExecuteReader();
> GetStuffFromReader(reader); // This one should be fine
> }
> }
> GetStuffFromReader(reader); // This one is not ok, since the
> connection is disposed
>
> }
>
> On Sep 24, 2:50 am, Benj Nunez <[EMAIL PROTECTED]> wrote:
>
> > Hello everyone,
>
> > Just out of curiosity: Is it alright to pass a datareader object to a
> > method or not?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web
Services,.NET Remoting" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://cm.megasolutions.net/forums/default.aspx
-~----------~----~----~----~------~----~------~--~---