How to properly close ODP.net connection : dispose() or close()?

前端 未结 4 515
梦谈多话
梦谈多话 2021-01-14 16:42

this is my powershell code :

[void][System.Reflection.Assembly]::LoadFile(\"C:\\DLL\\Oracle.ManagedDataAccess.dll\")
$OracleConnexion = New-Object Oracle.Ma         


        
4条回答
  •  旧时难觅i
    2021-01-14 17:16

    More standard implementation for resource which implement IDisposable is by wrapping it by using:

    using (OracleConnection connection = new OracleConnection(connectionString)){
        using (OracleCommand command = new OracleCommand(sql, connection))
            using (OracleDataReader reader = cmd.ExecuteReader())
            {
            }
        connection.Close(); //optional
    }
    

    It is equivalent to implementing .Dispose after the execution block. Internally, Dispose will also handle the closing. Nevertheless, you could also call the .Close() after the command block.

    A sample in the oracle documentation which uses Oracle.DataAccess encourages the use of Dispose too.

提交回复
热议问题