vbscript:测试是否存在记录集中的列
问题描述:
Bah,vbscript。vbscript:测试是否存在记录集中的列
我试图找出如何得到这个说法的工作:
if (not rsObject("columnNameThatDoesntExist") is nothing) then
' do some stuff
end if
' else do nothin
凡rsObject是一个记录和columnNameThatDoesntExist是......嗯,你知道的。我正在寻找像rsObject.Columns.Contains(字符串)。但当然找不到它。
编辑:看起来像循环rsObject.Fields是一个选项,是只有这样,才能做到这一点?
答
我相信你一定有你的理由,但如果你不知道什么字段,你在呼唤从数据库回来,你总是可以使用上的错误继续下一步和对错误转到0忽略抛出错误。似乎是一个不错的方法给我,但它的工作
blnItWasntThere = True
On Error Resume Next
If (rsObject("columnNameThatDoesntExist") <> "") Then
blnItWasntThere = False
...
...
...
End If
On Error Goto 0
If blnItWasntThere Then
'handle this error'
End If
但随着中说,我想你会更担心你正重返神秘记录。
或使自己的功能
Function ColumnExists(objRS, Column)
Dim blnOutput, x
blnOutput = True
On Error Resume Next
x = objRS(Column)
If err.Number <> 0 Then blnOutput = False
On Error Goto 0
ColumnExists = blnOutput
End Function
答
无论looooop并检查它的存在,或者只是试图抓住它:
Dim oRs:Set oRs = Nothing
On Error Resume Next
Set oRs = rsObject("columnNameThatDoesntExist")
On Error Goto 0
If Not rsObject("columnNameThatDoesntExist" Is Nothing Then
' ...
End If
不,我知道哪些列都回来了,但我在后端做一些奇怪的事情来处理一组未来可能增长的列,然后为这些列名取值。这是疯狂的,神秘的和丑陋的。但不能改变它足以让它正确。 – jcollum 2009-02-04 22:42:27