我正在使用 Microsoft SQL Server 2005 存储过程在 HTML 表中发布记录。在 HTML 表中,我有以下字段的行:条目号、开放日期、描述和所有者。
有时候,db表中的owner字段会是NULL。发生这种情况时,我在与所有者对应的 HTML 表行中有 ASP response.write“N/A”。但是,我想避免这种情况,因为它似乎是多余的。相反,如果所有者字段在数据库中为 NULL,我希望有一种方法可以完全消除该表行。我该怎么做呢?我正在使用 Javascript、经典 ASP 和 SQL Server 2005。我的代码如下。注意 - 我是所有这一切的新手。谢谢。
'Declare Variables
Dim CN, RS, vOutputType, vSQL, vNumber, vOwner
'Connection from includes file
Set CN = GetDataConnection
vOutputType = Request.QueryString("ot")
If Request.QueryString("txtNumber") <> "" Then
vNumber = Rtrim(Request.QueryString("txtNumber"))
End If
If Request.QueryString("cboOwner") <> "" Then
vOwner = Rtrim(Request.QueryString("cboOwner"))
End If
If vNumber <> "" Or vOwner <> "" Then
vSQL = "spReport "
vSQL = vSQL & "@vNumber = '" & vNumber & "', "
vSQL = vSQL & "@vOwner = '" & vOwner & "'"
Set RS = CN.Execute(vSQL)
If IsObject(RS) Then
If Not RS.EOF Then%>
<table class="WebApps">
<tr>
<td width="5%"><h3>Entry #</h3></td>
<td width="5%"><h3>Open Date</h3></td>
<td width="5%"><h3>Description</h3></td>
<td width="5%"><h3>Owner</h3></td>
</tr>
<%RS.MoveFirst
Do While Not RS.EOF
%>
<tr>
<td><p><%= RS("ID")%></p></td>
<td><p><%= RS("OpenDate")%></p></td>
<td><p><%= RS("Description")%></p></td>
<td><p><%If (RS("OwnerName")) <> "" Then Response.Write(RS("OwnerName")) Else Response.Write("N/A")%></p></td>
</tr>
<%RS.MoveNext
Loop%>
</table>
<%End If
End If
'Close objects
Set RS = NOTHING
CN.Close
Set CN = Nothing
最佳答案
由于您是“完全新手”,这里有一些其他建议。
SQL 注入(inject)攻击
你的代码:-
vSQL = "spReport "
vSQL = vSQL & "@vNumber = '" & vNumber & "', "
vSQL = vSQL & "@vOwner = '" & vOwner & "'"
Set RS = CN.Execute(vSQL)
不要这样做。有人可以创建查询字符串以在您的服务器上执行任意 SQL:-
?txtNumber=30&cboOwner='; arbitary SQL code here ; --
您应该始终使用 ADODB.Command 对象来执行需要从客户端检索参数值的 SQL。 Web 搜索“SQL 注入(inject) ASP”
NULL的含义
您似乎对 NULL 的含义有点困惑。 “数据库表中的所有者字段将为 NULL”,但您的代码正在测试 <>“”。 Null 与空字符串不同。实际上为 NULL 的字段将不等于“”。
使用 Server.HTMLEncode
你的代码:-
<td><p><%= RS("Description")%></p></td>
如果描述字段包含 <
会发生什么或 &
特点?更糟糕的是,如果描述字段中的数据源自 Web 客户端上的数据输入,它可能包含 Javascript 注入(inject)尝试。始终对发送到客户端的字符串数据使用 Server.HTMLEncode:-
<td><p><%= Server.HTMLEncode(RS("Description"))%></p></td>
您的实际问题
不要试图在您的 ASP 页面中使用 VBScript 来完成 SQL 应该完成的工作。 使用 WHERE 子句修改您的 SQL:-
WHERE Owner IS NOT NULL
或者如果您不确定 Owner 是 NULL 还是空字符串:-
WHERE Owner IS NOT NULL AND Owner <> ""
关于javascript - 根据 Recordset NULL 值隐藏 <TR> 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1689760/