经典ASP:如何使用SQL中的值两次?

时间:2009-02-09 23:35:26

标签: sql-server asp-classic

我对web dev很新鲜,但我正在整理一个带有经典ASP的简单软件目录。一切似乎都很好,除了我想在页面上两次使用我的SQL数据库中的值。例如,在页面标题和页面正文中,但我似乎只能使用每个值一次:

....
Set nItem = Request.QueryString("ID")

strSQL = "SELECT * "_
    & "FROM [Packages] "_
    & "WHERE id='" & nItem & "';" 

Set rstSearch = cnnSearch.Execute(strSQL)

<title><%=rstSearch.Fields("Software") %></title>
<body>
<center>Software Information</center>
<%=rstSearch.Fields("Software")%> <br />
<%=rstSearch.Fields("Version")%> <br />
<%=rstSearch.Fields("ID")%> <br />
<%=rstSearch.Fields("Licence")%> <br />
...

5 个答案:

答案 0 :(得分:5)

将其分配给变量;不要只从Recordset中提取值。

Set nItem = Request.QueryString("ID")

strSQL = "SELECT * "_
    & "FROM [Packages] "_
    & "WHERE id='" & nItem & "';" 

Set rstSearch = cnnSearch.Execute(strSQL)

Dim software
software = rstSearch("Software")
' set your other fields as variables...

<title><%= software %></title>
<body>
<center>Software Information</center>
<%= software %> <br />

应该适合你。

答案 1 :(得分:3)

首先是一个快速警告,您可以在查询中包含nItem时使用SQL注入。只是旁边,但要观看一个:)

除此之外,在记录集中引用两次列应该没有问题。我猜你在代码中遇到了不同的问题。如果您发布尝试此页面时获得的行为/错误,这可能会有所帮助。但我认为可能还有一些其他代码在上面的代码段中没有看到导致问题。

帮助您运行控制测试的一件事就是用“选择软件,版本,ID,许可证”等替换“select *”。您可以通过数字序号引用该列,这可能有所帮助。

祝你好运。

答案 2 :(得分:1)

您可能只需要将值提取到本地Var并使用它两次。很久很久以前,我曾经见过像这样奇怪的东西。

答案 3 :(得分:1)

一些意见:

1. Possibly call rstSearch.MoveFirst before the second 
   rstSearch.Fields("Software")
2. If that doesn't work write <%=Err.Description%> right after the 
   second rstSearch.Fields("Software") line.
3. Try not to ever use Select * for selecting columns. 
   Always specify which columns you want.
4. Please try and use ASP.Net. It's much better then ASP3.

答案 4 :(得分:0)

抱歉,对实际答案无能为力,但这看起来像SQL注入攻击的主要示例:

Set nItem = Request.QueryString("ID")

strSQL = "SELECT * "_
    & "FROM [Packages] "_
    & "WHERE id='" & nItem & "';" 

看起来我可以将URL更改为“?ID = 1”; DROP TABLE STUDENTS; - “将内容搞砸。