Closing connection object
After closing my data reader, how can i close the connection object forcefully?
2 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
SenthilkumarRank #387 · 6.5K reputationPosted Mar 1, 2012, 7:53 PM EST
The SqlCommand object allows you to define the command behaviour which will implicitly close the connection after read the recordset.
SqlCommand sqlCommand = new SqlCommand();
Hopefully this will address your question.
Jignesh TrivediRank #16 · 50.3K reputationPosted Mar 1, 2012, 10:17 PM EST
you can try...
string queryString="ur query";
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand command = new SqlCommand(queryString, connection);
connection.Open();
SqlDataReader reader = command.ExecuteReader();
// Call Read before accessing data.
while (reader.Read())
{
//do ur code
}
reader.Close();
}
using block automatically unrefernace ur connection from memory.
hope this help.