过滤掉objCom.CommandText中返回的NULL字段

时间:2015-09-16 19:00:06

标签: vbscript asp-classic

使用下面的脚本,我能够按预期列出OU中的用户,但输出会列出包括自定义ipphone字段中缺少数据的用户在内的所有内容。我需要帮助来修改代码,以便在ipphone字段中列出缺少分机号码的用户。 &#34;这表示用户已离开公司,不应出现在电话列表中<#34;

<%@ Language=VBScript  %>
<% response.Buffer = True %>
<html><head>
<title></title>
</head>
<body>
<h1>Directory</h1>
<%
' Define the AD OU that contains our users

usersOU = "LDAP://OU=IT,OU=Hollister,OU=Houston,OU=NFSmith,DC=nfsmith,DC=info"

' Make AD connection and run query
Set objCon = Server.CreateObject("ADODB.Connection")
objCon.provider ="ADsDSOObject"

objCon.Properties("User ID") = "domain\user"
objCon.Properties("Password") = "password"
objCon.Properties("Encrypt Password") = TRUE
objCon.open "Active Directory Provider"

Set objCom = CreateObject("ADODB.Command")
Set objCom.ActiveConnection = objCon
objCom.CommandText ="select givenName,sn,telephonenumber,ipphone FROM '"+ usersOU +"' where ipphone='*' ORDER by givenName"

Set objRS = objCom.Execute

' Loop over returned recordset and output HTML

Response.Write "<table>" + vbCrLf
Do While Not objRS.EOF Or objRS.BOF
    Response.Write "  <tr>"
    Response.Write "<td>" + objRS("givenName") + "</td>"
    Response.Write "<td>" + objRS("sn") + "</td>"

    Response.Write "<td>" + objRS("telephonenumber") + "</td>"
    Response.Write "<td>" + objRS("IPphone") + "</td>"

    Response.Write "</tr>" + vbCrLf
    objRS.MoveNext
    Response.Flush
Loop
Response.Write "</table>"
' Clean up
objRS.Close
objCon.Close
Set objRS = Nothing
Set objCon = Nothing
Set objCom = Nothing
%>
</body>
</html>

2 个答案:

答案 0 :(得分:0)

在写入输出之前检查值是否为Null

Do Until objRS.EOF
    If Not IsNull(objRS("ipPhone")) Then
        Response.Write "  <tr><td>" + objRS("givenName")
        Response.Write "</td><td>" + objRS("sn")
        Response.Write "</td><td>" + objRS("telephoneNumber")
        Response.Write "</td><td>" + objRS("ipPhone")
        Response.Write "</td></tr>" & vbCrLf
        Response.Flush
    End If

    objRS.MoveNext
Loop

答案 1 :(得分:0)

如果您根本不想要NULL记录,为什么不告诉您的SQL查询将它们排除?

所以你在哪里

objCom.CommandText ="select givenName,sn,telephonenumber,ipphone FROM '"+ _
   usersOU +"' where ipphone='*' ORDER by givenName"

添加条件:

objCom.CommandText = "SELECT givenName, sn, telephonenumber, ipphone FROM '" & _
    usersOU & "' WHERE ipphone = '*' AND ipphone IS NOT NULL ORDER BY givenName"

(我不确定ipphone = '*'的用途是什么,但我不太了解Active Directory。)

另一种方法,即在代码中测试IsNull(objRS("ipPhone")),如果你想在字段为空时做其他事情,那么它很有用 - 也许可以添加一个“非活动”类,这类事情。