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

前端 未结 4 524
梦谈多话
梦谈多话 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条回答
  •  醉酒成梦
    2021-01-14 17:15

    I would wrap your connection by a using statement. When you're done with your connection then close it before adding the bracket. To be 100% safe I would do something like this:

    using(OracleConnexion Con = new OracleConnection (...))
    {
        Con.Open()
        ...
        Con.Close()
    }
    

    Edit:

    I added the Con.Close(), because in the past the dispose was not implemented correctly in the ODP.NET. The connection was kept open. We had to force the connection to close manually and that's why in the example, I specify the Close.

提交回复
热议问题