VBScript:如何将记录集的值设置为字符串

发布于 2024-07-08 20:04:58 字数 1332 浏览 11 评论 0原文

这可能是一个初学者问题,但是如何将记录集设置为字符串变量?

这是我的代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

甜宝宝 2024-07-15 20:04:58

试试这个:


getOffice = objRS.getString

这将以制表符分隔的字符串形式返回整个记录集。

Try This:


getOffice = objRS.getString

This will return the entire recordset as a tab delimited string.

自此以后,行同陌路 2024-07-15 20:04:58

Set 只能用于对象,不能用于字符串等简单变量。
试试这个:(它还确保记录集不为空)


If objRS.RecordCount <> 0 Then
  getOffice = CStr(objRS.Fields(0))
Else
  getOffice = ""
End If

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)


If objRS.RecordCount <> 0 Then
  getOffice = CStr(objRS.Fields(0))
Else
  getOffice = ""
End If
别把无礼当个性 2024-07-15 20:04:58
Cstr(objRS.Fields(0))
Cstr(objRS.Fields(0))
梦里南柯 2024-07-15 20:04:58

尝试更改此设置:

将 getOffice = objRS.Fields(0) 设置

为:

getOffice = objRS.Fields(0)

Try changing this:

Set getOffice = objRS.Fields(0)

to this:

getOffice = objRS.Fields(0)

就是爱搞怪 2024-07-15 20:04:58

根据我的经验,从数据库调用返回数据的各种方式通常非常依赖于用于访问数据的方法/驱动程序(例如 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.

街道布景 2024-07-15 20:04:58

我刚刚在顶部添加了“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

执手闯天涯 2024-07-15 20:04:58

您确定您的记录集不为空吗? 通常,在开始执行任何操作之前,您需要先进行检查。 由于不知道它需要做什么,我可能会建议这样的事情:

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

显然有很多方法可以检查空记录集和检查空值等,但这里有一种可能有效的方法。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文