Closing connection object
After closing my data reader, how can i close the connection object forcefully?
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Jignesh TrivediPosted Mar 2, 2012, 3:17 AM
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.
SenthilkumarPosted Mar 2, 2012, 12:53 AM
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.