一个很简单的问题,却往往被忽略,让我不知所措.关于rs记录集关闭的位置?大家帮看看阿!
不老书生 2005-06-17 04:10:39 我原来的代码是这样的
Function GetColumnValue() 行1
Dim Conn As ADODB.Connection 行2
Dim Rs As ADODB.Recordset 行3
On Error GoTo ErrorHandle 行4
Set Conn = New ADODB.Connection 行5 '?放在这个位置对不对
Set Rs = New ADODB.Recordset 行6 '?放在这个位置对不对
Conn.ConnectionString = commonYtth.getConnectionString() 行7
Conn.Open
sql = "SELECT " & NameColumnName & " from " & tableName & " where " & CodeColumnName & "='" & CodeColumnValue & "'" 行8
Rs.Open sql, Conn, 3, 2 行9
if ... then 行10
GetColumnValue="0" 行11
exit function 行12
else 行13
GetColumnValue="1" 行14
end if 行15
...
Rs.Close 行16
Set Rs = Nothing 行17
Conn.Close 行18
Set Conn = Nothing 行19
ErrorHandle: 行20
GetColumnValue = "0" 行21
Rs.Close 行22
Set Rs = Nothing 行23
Conn.Close 行24
Set Conn = Nothing 行25
End Function 行26
如果行5---行19的过程中出现错误 则直接跳到行20执行,21--25把记录关闭,但是系统报错,必须去掉rs.close,conn.close才行
我要怎样才能保证任何情况下记录集都能被关闭阿?