string VBScript:如何将记录集中的值设置为字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/303745/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VBScript: how to set values from recordset to string
提问by phill
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.
当我尝试运行该函数时,它会抛出错误“vbscript 运行时错误:类型不匹配”,我认为这意味着它无法使用记录集值设置字符串变量。
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
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.
这会引发不同的错误 ADODB.Field:BOF 或 EOF 为 True,或者当前记录已被删除。请求的操作需要当前记录。
回答by Tester101
Try This:
尝试这个:
getOffice = objRS.getString
This will return the entire recordset as a tab delimited string.
这将以制表符分隔的字符串形式返回整个记录集。
回答by Tester101
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)
Set 仅用于对象,不能用于字符串等简单变量。
试试这个:(它还确保记录集不为空)
If objRS.RecordCount <> 0 Then
getOffice = CStr(objRS.Fields(0))
Else
getOffice = ""
End If
回答by BQ.
Cstr(objRS.Fields(0))
回答by EBGreen
Try changing this:
尝试改变这个:
Set getOffice = objRS.Fields(0)
设置 getOffice = objRS.Fields(0)
to this:
对此:
getOffice = objRS.Fields(0)
getOffice = objRS.Fields(0)
回答by BQ.
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.
根据我的经验,从 DB 调用返回数据的各种方式通常非常依赖于用于访问数据的方法/驱动程序(例如 ODBC、ADO、ADO.NET、ODP.NET、OleDB 等)。需要 ToString()、GetString()、强制转换或其他一些变体。
回答by phill
I just ended up adding "On Error Resume Next" to the top and it just skips the null errors.
我刚刚在顶部添加了“On Error Resume Next”,它只是跳过了空错误。
although I wish there was an easier way to handle NULL values in vbscript.
虽然我希望有一种更简单的方法来处理 vbscript 中的 NULL 值。
thanks for all your help
感谢你的帮助
回答by AnonJr
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:
你确定你的记录集不是空的?通常你会想在开始做任何事情之前先检查一下。对它需要做什么一无所知,我可能会建议这样的事情:
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
If Not objRS.BOF Then objRS.Move First
If Not objRS.EOF Then
If Not IsNull(objRS.Fields(0)) and objRS.Fields(0) <> "" Then WScript.Echo cStr(objRS.Fields(0))
End If
Set cmd = Nothing
Set cn = Nothing
Set objDomain = Nothing
Set objRoot = Nothing
Dim arStore
Set getOffice = objRS.Fields(0)
Set objRS = Nothing
End function
Obviously there are many ways of checking for an empty recordset, and checking for nulls, etc. but here's one way that may work.
显然,有很多方法可以检查空记录集和检查空值等,但这里有一种可能有效的方法。