Interviews6 answers
How can we force the connection object to close after my datareader is closed ?
6 Answers
1.we can use using() function it automatically close the connection after executing the query
2 .cmd.ExecuteReader(CommandBehavior.CloseConnection)
Vamsi KrishnaPosted Oct 7, 2015, 3:57 AM EDT
There are 2 ways through which we can achieve this:
(01.) Through "using()".
(02.) Through passing "CommandBehavior.CloseConnection" as parameter to command object "ExecuteReader()"
Sujeet SumanPosted Jun 18, 2015, 2:59 AM EDT
user using() to free the memory allocated for resource .
Santhosh SubramaniamPosted Apr 16, 2015, 5:48 AM EDT
We can use "CommandBehaviour.CloseConnection" Like as :
SqlCommand cmd = new SqlCommand("SELECT * FROM EMPLOYEE", con);
con.Open();
SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
while (reader.Read())
{
int weight = reader.GetInt32(0); // Weight int
string name = reader.GetString(1); // Name string
string breed = reader.GetString(2); // Breed string
//
// Write the values read from the database to the screen.
//
Response.Write(weight.ToString() + name + breed);
}
reader.Close();
Pankaj Kumar ChoudharyPosted Feb 20, 2015, 4:24 AM EST
Using CommandBehaviour we can do
objcommand.ExecuteReader(CommandBehaviour.ClossConnection)
saravanagopi sPosted Feb 18, 2015, 10:00 PM EST
Command method Executereader takes a parameter called as CommandBehavior wherein we can specify saying close connection automatically after the Datareader is close.
pobjDataReader = pobjCommand.ExecuteReader(CommandBehavior.CloseConnection)
Nitin ChoudharyPosted Feb 11, 2015, 3:20 AM EST



