如何最有效的使用ADODB.RecordSet?
我们都知道ADODB。RecordSet有以下两种写法
dim recTemp as adodb.recordeset
set recTemp = new adodb.recoordset
与
dim recTemp as new adodb.recordest
'为此,我做了如下的测试程序
Option Explicit
Private AdoRs1 As ADODB.Recordset
Private AdoRs2 As New ADODB.Recordset
Private Sub CmdTest_Click()
'Step1: 新建一个ADODB.RecordSet实例; 注意 AdoRs2不需要新建
Set AdoRs1 = New ADODB.Recordset
'Step2: 执行某些操作
MsgBox "AdoRs1字段个数 :" & AdoRs1.Fields.Count
MsgBox "AdoRs2字段个数 :" & AdoRs2.Fields.Count
'Step3: 释放RecordSet
If AdoRs1.State = adStateOpen Then
AdoRs1.Close
End If
Set AdoRs1 = Nothing
If AdoRs2.State = adStateOpen Then
AdoRs2.Close
End If
Set AdoRs2 = Nothing
'Step4: 测试被释放后的RecordSet (注意测试的顺序)
If AdoRs1 Is Nothing Then
MsgBox "AdoRs1 被完全释放 !"
Else
MsgBox "AdoRs1 没有被完全释放 !"
End If
If AdoRs2 Is Nothing Then
MsgBox "AdoRs2 被完全释放 !"
Else
MsgBox "AdoRs2 没有被完全释放 !"
End If
MsgBox "AdoRs2字段个数 :" & AdoRs2.Fields.Count
MsgBox "AdoRs1字段个数 :" & AdoRs1.Fields.Count
End Sub
'经过以上测试后, 我们发现一个有趣的现象:
'AdoRs1被释放后不能读取AdoRs1的某些属性 , 必须重新执行
'Set AdoRs1 = New ADODB.Recordset 方可使用某些属性
'这样可能造成某些不方便
'AdoRs2被释放后仍然能读取AdoRs2的某些属性
'这样是否会造成资源没有被完全释放,造成对程序运行效率的影响
'希望大家能对该问题进行一些讨论和指教,谢谢。