Is it necessary to close an Adodb.recordset object before setting it to nothing?

后端 未结 4 1476
猫巷女王i
猫巷女王i 2021-02-13 19:05
Dim rs as ADODB.Recordset
set rs = ReturnARecordset \'assume ReturnARecordset does just that...

\'do something with rs

rs.Close
set rs = Nothing

Is i

4条回答
  •  余生分开走
    2021-02-13 20:08

    The only reason calling Close explicitly is when you are not sure if the recordset is referenced from somewhere else in your project, usually a result of some sloppy coding.

    Dim rs as ADODB.Recordset
    Set rs = ReturnARecordset
    ...
    MyControl.ObscureMethod rs
    ...
    Set rs = Nothing
    

    Last line is supposed to terminate the recordset instance without calling Close explicitly, unless MyControl is holding an extra reference and thus preventing normal tear-down. Calling Close on rs will make sure MyControl cannot use its reference for anything useful, crashing in flames in the meantime.

提交回复
热议问题