How to dispose ADODB.Command object in code migrated to VB.NET

£可爱£侵袭症+ 提交于 2019-12-11 10:19:20

问题


We migrated some obsolete code from VB6 to VB.net and the previous code used ADODB for connecting to oracle. Below is how the code looks like

Dim cmd As New ADODB.Command

' stored procedures execution here

cmd = Nothing    // I want to dispose this cmd Object 

Nothing worked in VB6 but its creating lot of trouble with .NET there are open cursors on database. I know we should use the latest library and get rid of ADODB but for now is there a way I can dispose this object. I tried cmd.Dispose() but that did not worked.


回答1:


Use Marshal.ReleaseComObject to tidy up COM objects. ADODDB is a COM library being used through COM Interop.

COM objects being used through COM-Interop will not have a Dispose method, but may still need to be tidied up manually. Contrast with "proper" .Net objects will likely have a Dispose method, or implement IDisposable.




回答2:


In VB.net (or C# for that matter), classes which should be disposed should include a Dispose() Method. Classes which do not need to be disposed should not include a Dispose() method.

In your case, the Command instance may not need to be disposed, but I would wager that the Connection instance (which you haven't included in your example code, but I'm pretty sure you've got) does.



来源:https://stackoverflow.com/questions/22383993/how-to-dispose-adodb-command-object-in-code-migrated-to-vb-net

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!