Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#2.0 C#3.0 C# ASP.NET C#4.0 , +
Hi all
 
public void test()
{
      MySqlDataReader oReader = myExecuteReader(selectQuery);
      
      oReader.Close();
 
}
 

public MySqlDataReader myExecuteReader(string SQL_STRING)
		{	
			//Function to Accept a SQL String and Return a Data Reader
			//Declare Objects
			MySqlConnection oCn = null;
			MySqlCommand oCmd = null;
			MySqlDataReader oRdr = null;
 
            try
            {
                //Open Connection
                oCn = new MySqlConnection(ConfigurationSettings.AppSettings["SQLConnectionString"]);
                oCn.Open();
 
                //Open the MySqlCommand object
                oCmd = new MySqlCommand(SQL_STRING, oCn);
 
                //Return the Data Reader to the callee
                oRdr = oCmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
 
            }
            catch (Exception ex)
            {
                ExceptionLogger.LogException(ex);
            }
			
			return oRdr;
					
		}	//End Function 
 

My question is when i close the oReader Reader than is it will close the reader present inside the function???
 
or it will remains open???
 
Please suggest
 
Thanks in advance
Posted 8-Jun-12 6:06am
Mac123341.1K
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

No.
The oReader inside the "test" method is a completely separate instance.
  Permalink  
Comments
MAC123456 at 8-Jun-12 20:00pm
   
In this case it is which type(reference type or value type)??
 
I have written the in my program and don't know which type please suggest.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Assuming MySqlDataReader is a reference type, yes closing it in test will close the one inside of myExecuteReader, because it's a reference to the same MySqlDataReader. (If MySqlDataReader is a value type then this would not be true, as you would instead be working with a copy.)
  Permalink  
Comments
MAC123456 at 8-Jun-12 20:00pm
   
In this case it is which type(reference type or value type)??
 
I have written the in my program and don't know which type please suggest.
lewax00 at 8-Jun-12 20:13pm
   
According the the documentation here: http://www.devart.com/dotconnect/mysql/docs/Devart.Data.MySql~Devart.Data.MySql.MySqlDataReader.html it's a class, and therefore a reference type. (classes are reference types, structs are value types in C#)
MAC123456 at 8-Jun-12 20:14pm
   
means it is closing the connection??
 
lewax00 at 8-Jun-12 20:16pm
   
Yup. When you return it from the function what you really return is a reference to the object, so you're calling close on the same object you created in the function.
MAC123456 at 8-Jun-12 20:21pm
   
Thanks a ton.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

if you have passed it as value type then it will not be closed, but you passed it as referenced type then it will closed.
  Permalink  
Comments
MAC123456 at 8-Jun-12 20:00pm
   
In this case it is which type(reference type or value type)??
 
I have written the in my program and don't know which type please suggest.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 375
1 Sergey Alexandrovich Kryukov 329
2 CPallini 260
3 DamithSL 214
4 Maciej Los 197
0 OriginalGriff 5,455
1 DamithSL 4,457
2 Maciej Los 3,885
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,115


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 8 Jun 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100