use a try...catch statement to force a connection to close : using « ADO.net Database « ASP.NET Tutorial






SqlConnection con = new SqlConnection("Data Source=localhost;Integrated Security=True;Initial Catalog=Pubs");
SqlCommand cmd = new SqlCommand("INSERT Titles (Title) VALUES ('Some Title')", con);
try
{
  con.Open();
  cmd.ExecuteNonQuery();
}
finally
{
  con.Close();
}








18.59.using
18.59.1.The using statement forces the connection to close, regardless of whether there is an error
18.59.2.use a try...catch statement to force a connection to close