不必讨论如何构造代码等,这是一个足够简单的修复方法。
SELECT CASE CINT(Session("SITE_LVL"))
CASE 1 'Site
'Recordset instantiated here
Set rsGet_Market = Server.Createobject("ADODB.Recordset")
rsGet_Market.Open strQuery, objConn
'Lots of fluff here removed to emphasize the point
'...
'Close Recordset inside the Case statement
rsGet_Market.close
CASE 4 'Market
'Recordset instantiated here
Set rsGet_Market = Server.Createobject("ADODB.Recordset")
rsGet_Market.Open strQuery, objConn
'Lots of fluff here removed to emphasize the point
'...
'Close Recordset inside the Case statement
rsGet_Market.close
END SELECT
'Don't close rsGet_Market here as it might not exist and cause an error.
objConn.Close
通过移动
rsGet_Market.close
在Case语句中,只有当存在对应的
ADODB.Recordset
在
rstGet_Market
对象
无法关闭一开始就不存在的记录集。
但是,我们可以通过将实例化移到Case语句之外来消除更多的重复来改进这一点(
DRY Principle
)
'Recordset instantiated here
Set rsGet_Market = Server.Createobject("ADODB.Recordset")
SELECT CASE CINT(Session("SITE_LVL"))
CASE 1 'Site
'Recordset will be open if data is returned.
rsGet_Market.Open strQuery, objConn
'Lots of fluff here removed to emphasize the point
'...
'Close Recordset inside the Case statement
rsGet_Market.close
CASE 4 'Market
'Recordset will be open if data is returned.
rsGet_Market.Open strQuery, objConn
'Lots of fluff here removed to emphasize the point
'...
'Close Recordset inside the Case statement
rsGet_Market.close
END SELECT
'Release Recordset object from memory
Set rsGet_Market = Nothing
'Don't close rsGet_Market here as it might not exist and cause an error.
objConn.Close