Click here to Skip to main content
15,892,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi all
C#
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

No.
The oReader inside the "test" method is a completely separate instance.
 
Share this answer
 
Comments
Mac12334 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.
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.)
 
Share this answer
 
Comments
Mac12334 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 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#)
Mac12334 8-Jun-12 20:14pm    
means it is closing the connection??

lewax00 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.
Mac12334 8-Jun-12 20:21pm    
Thanks a ton.
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.
 
Share this answer
 
Comments
Mac12334 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900