ASP.NET Tutorial/ADO.net Database/using — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 11:57, 26 мая 2010
The using statement forces the connection to close, regardless of whether there is an error
using statement also disposes of the Connection object.
If you need to reuse the Connection, then you need to reinitialize it.
SqlConnection con = new SqlConnection("Data Source=localhost;Integrated Security=True;Initial Catalog=Pubs");
SqlCommand cmd = new SqlCommand("INSERT Titles (Title) VALUES ("Some Title")", con);
using (con)
{
con.Open();
cmd.ExecuteNonQuery();
}
use a try...catch statement to force a connection to close
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();
}