나는이 두 개체가 말 아니면 둘다?내가 .NET에서 OracleConnection을 닫습니다 어떻게
충분이 좋은가요 :이 정도면 충분
using(connection)
{
OracleDataReader reader = cmd.ExecuteReader();
// whatever...
}
나는이 두 개체가 말 아니면 둘다?내가 .NET에서 OracleConnection을 닫습니다 어떻게
충분이 좋은가요 :이 정도면 충분
using(connection)
{
OracleDataReader reader = cmd.ExecuteReader();
// whatever...
}
. using 문은 dispose 문을 감쌀 것이므로 예외가 발생하더라도 안전합니다. 리소스를 처리하는 것이 바람직한 방법입니다.
using(OracleConnection connection = new OracleConnection(connectionString); )
{
//Create a command object
using(OracleCommand command = new OracleCommand(sql, connection))
{
using(OracleDataReader reader = cmd.ExecuteReader())
{
}
}
// whatever...
}
내가 주입하는 컴파일러를 물어, 당신이 "사용"을 사용하여 생각 ... finally 블록 시도, 마지막 블록에, 그것은 당신을 위해 일회용 객체를 닫습니다.
using (OracleConnection connection = new OracleConnection(connectionString))
{
using (OracleCommand command = new OracleCommand(sql, connection))
{
using (OracleDataReader reader = cmd.ExecuteReader())
{
}
}
}
IDisposable을 구현하고 생성 한 경우 사용 블록에 넣습니다.
using
은 연결이 끊어지지 않도록합니다. 이 호출되기 전에 CommandBehavior.CloseConnection
을 명령의 ExecuteReader
메서드에 전달하여 닫을 수도 있습니다.
두 가지 대답은 거의 목표에 달려 있습니다. IDisposeable 개체에서 .Dispose()를 항상 호출하려고합니다. ""당신은 항상 try/finialy 블록 impliment 당신이 키가 컴파일러를 사용하여 래핑. 당신이 중첩을 피하려면
1 노트의 포인트는,이 같은 같은 코드를 작성할 수 있습니다
using (OracleConnection connection = new OracleConnection(connectionString))
using (OracleCommand command = new OracleCommand(sql, connection))
using (OracleDataReader reader = cmd.ExecuteReader())
{
// do something here
}
실제로 필요한 내부 using이됩니다, 또는 '연결'충분의 하나는? –
IDisposable 개체를 모두 처분해야하므로 각 사용이 필요합니다. 당신이하지 않으면 당신은 연결을 처분하고, 아이들은 떠날 것입니다. – Russ
데이터베이스 커넥션 (그리고 일반적으로 ID 생성 가능)에 대해 허가되지만, 부모를 처분하는 것이 자식을 처분하는 입/출력 스트림을 계단식으로 처리하는 것은 불가능합니다. – Mac