我正在通过 ADO 对数据库表进行大量调用。本着保持 DRY 的精神,我编写了以下函数来从记录集中返回一组值。这是兔子脑子吗?我主要使用它来获取一组组合框值等,从不用于巨大的值。示例用法(为简洁起见删除了错误处理):
Function getEmployeeList()
getEmployeeList= Array()
strSQL = "SELECT emp_id, emp_name from employees"
getEmployeeList = getSQLArray( strSQL, "|" )
End Function
然后我只是对返回的数组做任何我想做的事情。
Function getSQLArray( SQL, delimiter )
'*************************************************************************************
' Input a SQL statement and an optional delimiter, and this function
' will return an array of strings delimited by whatever (pipe defaults)
' You can perform a Split to extract the appropriate values.
' Additionally, this function will return error messages as well; check for
' a return of error & delimiter & errNum & delimiter & errDescription
'*************************************************************************************
getSQLArray = Array()
Err.Number = 0
Set objCon = Server.CreateObject("ADODB.Connection")
objCon.Open oracleDSN
Set objRS = objCon.Execute(SQL)
if objRS.BOF = false and objRS.EOF = false then
Do While Not objRS.EOF
for fieldIndex=0 to (objRS.Fields.Count - 1)
If ( fieldIndex <> 0 ) Then
fieldValue = testEmpty(objRS.Fields.Item(fieldIndex))
recordString = recordString & delimiter & fieldValue
Else
recordString = CStr(objRS.Fields.Item(fieldIndex))
End If
Next
Call myPush( recordString, getSQLArray )
objRS.MoveNext
Loop
End If
Set objRS = Nothing
objCon.Close
Set objCon = Nothing
End Function
Sub myPush(newElement, inputArray)
Dim i
i = UBound(inputArray) + 1
ReDim Preserve inputArray(i)
inputArray(i) = newElement
End Sub
Function testEmpty( inputValue )
If (trim( inputValue ) = "") OR (IsNull( inputValue )) Then
testEmpty = ""
Else
testEmpty = inputValue
End If
End Function
我的问题是:像这样将所有记录集对象的创建/打开/错误处理抽象到它自己的函数调用中是否有意义?我是否正在建造一台 Rube Goldberg 机器,任何维护此代码的人都会诅咒我的名字?
我应该把它吸起来并编写一些宏来吐出 ADO 连接代码,而不是尝试在函数中执行它吗?
我对 asp 很陌生,所以我在它的功能/最佳实践中存在漏洞,所以任何输入都将不胜感激。