데이터베이스 연결을 사용하고 닫는 일반적인 방법은 무엇인지 물어보고 싶습니다.연결이 닫혀 있는지 확인
이것은 내 프로그램이지만 예외 인 connection.Close()가 실행되지 않습니다.
전체 블록에 try-catch를 사용해야합니까? 왜냐하면 어떤 이유로 나는 대부분의 사람들이 나던 것을 본다.
using (SqlConnection connection = new SqlConnection(ConnectionString))
{
using (SqlCommand command = new SqlCommand())
{
command.CommandText = "procedure";
command.Connection = connection;
command.CommandType = CommandType.StoredProcedure;
tmpParameter = DBUtils.createInSQLParam("@ImageID", SqlDbType.SmallInt, htmlImageId);
command.Parameters.Add(tmpParameter);
command.Connection.Open();
using (SqlDataReader reader = command.ExecuteReader())
{
htmlImageDetails = GetHtmlImageDetailsFromReader(reader, true, out newId);
reader.Close();
}
connection.Close();
return htmlImageDetails;
}
}
예외는 무엇입니까? –
이 질문에 대한 답변을 것 같습니다 - http://stackoverflow.com/a/4717802/978528. – nickfinity
"사용"을 사용할 때 연결이 닫히지 않습니다/판독기가 자동으로 닫힙니다. .net은 기본적으로 연결 풀링을 사용하므로 연결을 재사용하기 위해 풀에 배치하면 연결이 닫히지 않습니다. 이렇게하면 대부분의 경우 성능이 향상됩니다. – Peter