VBScript:如何将记录集的值设置为字符串
这可能是一个初学者问题,但是如何将记录集设置为字符串变量?
这是我的代码:
Function getOffice (strname, uname)
strEmail = uname
WScript.Echo "email: " & strEmail
Dim objRoot : Set objRoot = GetObject("LDAP://RootDSE")
Dim objDomain : Set objDomain = GetObject("LDAP://" & objRoot.Get("defaultNamingContext"))
Dim cn : Set cn = CreateObject("ADODB.Connection")
Dim cmd : Set cmd = CreateObject("ADODB.Command")
cn.Provider = "ADsDSOObject"
cn.Open "Active Directory Provider"
Set cmd.ActiveConnection = cn
cmd.CommandText = "SELECT physicalDeliveryOfficeName FROM '" & objDomain.ADsPath & "' WHERE mail='" & strEmail & "'"
cmd.Properties("Page Size") = 1
cmd.Properties("Timeout") = 300
cmd.Properties("Searchscope") = ADS_SCOPE_SUBTREE
Dim objRS : Set objRS = cmd.Execute
WScript.Echo objRS.Fields(0)
Set cmd = Nothing
Set cn = Nothing
Set objDomain = Nothing
Set objRoot = Nothing
Dim arStore
Set getOffice = objRS.Fields(0)
Set objRS = Nothing
End function
当我尝试运行该函数时,它会抛出错误“vbscript 运行时错误:类型不匹配” 我认为这意味着它无法使用记录集值设置字符串变量。
我该如何解决这个问题?
我只是尝试
if IsNull(objRS.Fields(0).Value) = TRUE 那么 getOOffice =“noAD” 别的 getOOffice = objRS.Fields(0).VALue end if
这会引发不同的错误 ADODB.Field: 要么 BOF 或 EOF 为 True,要么当前记录已被删除。 请求的操作需要当前记录。
This is probably a beginner question, but how do you set a recordset to a string variable?
Here is my code:
Function getOffice (strname, uname)
strEmail = uname
WScript.Echo "email: " & strEmail
Dim objRoot : Set objRoot = GetObject("LDAP://RootDSE")
Dim objDomain : Set objDomain = GetObject("LDAP://" & objRoot.Get("defaultNamingContext"))
Dim cn : Set cn = CreateObject("ADODB.Connection")
Dim cmd : Set cmd = CreateObject("ADODB.Command")
cn.Provider = "ADsDSOObject"
cn.Open "Active Directory Provider"
Set cmd.ActiveConnection = cn
cmd.CommandText = "SELECT physicalDeliveryOfficeName FROM '" & objDomain.ADsPath & "' WHERE mail='" & strEmail & "'"
cmd.Properties("Page Size") = 1
cmd.Properties("Timeout") = 300
cmd.Properties("Searchscope") = ADS_SCOPE_SUBTREE
Dim objRS : Set objRS = cmd.Execute
WScript.Echo objRS.Fields(0)
Set cmd = Nothing
Set cn = Nothing
Set objDomain = Nothing
Set objRoot = Nothing
Dim arStore
Set getOffice = objRS.Fields(0)
Set objRS = Nothing
End function
When I try to run the function, it throws an error "vbscript runtime error: Type mismatch"
I presume this means it can't set the string variable with a recordset value.
How do I fix this problem?
I just tried
if IsNull(objRS.Fields(0).Value) = TRUE then
getOFfice = "noAD"
else
getOFfice = objRS.Fields(0).VAlue
end if
And that throws a different error ADODB.Field: Either BOF or EOF is True, or the current record has been deleted. Requested operation requires a current record.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
试试这个:
这将以制表符分隔的字符串形式返回整个记录集。
Try This:
This will return the entire recordset as a tab delimited string.
Set 只能用于对象,不能用于字符串等简单变量。
试试这个:(它还确保记录集不为空)
Set is only used for objects, it cannot be used on simple variables like strings.
Try this: (it also makes sure the recordset is not empty)
尝试更改此设置:
将 getOffice = objRS.Fields(0) 设置
为:
getOffice = objRS.Fields(0)
Try changing this:
Set getOffice = objRS.Fields(0)
to this:
getOffice = objRS.Fields(0)
根据我的经验,从数据库调用返回数据的各种方式通常非常依赖于用于访问数据的方法/驱动程序(例如 ODBC、ADO、ADO.NET、ODP.NET、OleDB 等)。需要 ToString()、GetString()、强制转换或其他一些变体。
It's been my experience that the various ways of returning data from a DB call are often times very dependent on the method/driver used to access the data (e.g. ODBC, ADO, ADO.NET, ODP.NET, OleDB, etc.) Some need ToString(), GetString(), a cast, or some other variant of that.
我刚刚在顶部添加了“On Error Resume Next”,它只是跳过了空错误。
尽管我希望有一种更简单的方法来处理 vbscript 中的 NULL 值。
感谢你的帮助
I just ended up adding "On Error Resume Next" to the top and it just skips the null errors.
although I wish there was an easier way to handle NULL values in vbscript.
thanks for all your help
您确定您的记录集不为空吗? 通常,在开始执行任何操作之前,您需要先进行检查。 由于不知道它需要做什么,我可能会建议这样的事情:
显然有很多方法可以检查空记录集和检查空值等,但这里有一种可能有效的方法。
Are you sure your recordset isn't empty? Normally you'd want to check first before you started doing anything. Not knowing anything else about what it needs to do, I might suggest something like this:
Obviously there are many ways of checking for an empty recordset, and checking for nulls, etc. but here's one way that may work.