如何测试ASP中的空SQL结果

问题描述:

我正在使用MySQL数据库从ASP运行查询,我想根据结果与人员姓名(全名)创建一个变量(ssResult),如果记录确实不存在我想文本“N/A”到可变分配,下面的代码,我目前使用的功能getOther为其传递列名“全名”我的数据库连接:如何测试ASP中的空SQL结果

ssResult = getOtherElse("SELECT fullname FROM table WHERE id=" & inArr(j), "fullname") 

下面是代码函数getOtherElse只适用于返回结果但不适用于空结果的情况:

Function getOtherElse(inSQL, getColumn) 
    Dim conn, rstemp 
    Set conn = Server.CreateObject("ADODB.Connection") 
    conn.open myDSN 
    Set Session("lp_conn") = conn 
    Set rstemp = Server.CreateObject("ADODB.Recordset") 
    rstemp.Open inSQL, conn 
    if not rstemp.eof then 
     rstemp.movefirst 
     getOtherElse=rstemp.fields(getColumn) 
    else 
     getOtherElse="N/A" 
    end if 
    rstemp.close 
    set rstemp=nothing 
    conn.close 
    set conn=nothing 
End Function 

谢谢!

+0

此代码是脆弱的到sql注入 – 2011-12-01 20:15:29

你可以尝试转产

if not rstemp.eof then 

if rstemp.RecordCount > 0 then 

为什么不改变SQL只拉出一个名字,这样的结果, “N/A” 将适用:

sResult = getOtherElse("SELECT fullname FROM table WHERE id=" & inArr(j), "fullname AND fullname<>''")
+0

有没有一个名字叫“全名和全名”'“ – 2008-11-25 14:19:27

+0

对不起,应该是全名''尖括号没有在后 – Katy 2008-11-25 15:09:15

替换的代码块:

if not rstemp.eof then 
    rstemp.movefirst 
    getOtherElse=rstemp.fields(getColumn) 
else 
    getOtherElse="N/A" 
end if 

用的代码块:

Dim output 
output = "N/A" 

If Not rstemp.eof Then 
    rstemp.movefirst 
    value = rstemp.fields(getColumn) 

    If trim(value) = "" Then 
     value = "N/A" 
    End If 
End If 

getOtherElse = value 

上面的代码总是假定被返回什么都没有,直到连接ACTU盟友将其设置为返回。然后该值被检查为空字符串,并将其值也为“N/A”