问题
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