【问题标题】:unable to execute query in ASP无法在 ASP 中执行查询
【发布时间】:2012-04-24 20:37:00
【问题描述】:

以下是我正在处理的 ASP 代码:

Dim sConnString, connection, sSQL, backgroundColor
sSQL = "SELECT firstName, lastName, email, zip, country, company, industry, revenue, timestamp FROM users"
sConnString=//a connection string
Set connection = Server.CreateObject("ADODB.Connection")
connection.Mode=adModeRead
connection.Open(sConnString)
connection.execute(sSQL)

Response.Write"<table>"
do while not connection.EOF
    if(backgroundColor="#f1f1f1")then
        backgroundColor="#ffffff"
    else
        backgroundColor="#f1f1f1"
    end if

    response.write"<tr backgroundColor="& backgroundColor & "></td>" & connection("firstName") & "</td><td>" & connection("lastName") & "</td><td>" & connection("email") & "</td><td>" & connection("zip") & "</td><td>" & connection("country") & "</td><td>" & connection("company") & "</td><td>" & connection("industry") & "</td><td>" & connection("revenue") & "</td><td>" & connection("timestamp") & "</td></tr>"

    connection.MoveNext
Loop
Response.Write"</table>"

connection.Close
Set connection = Nothing

我收到以下错误:

ADODB.Connection 错误'800a0bb9'

参数类型错误、超出可接受范围或相互冲突。

/wsu.asp,第 13 行

第 13 行:

do while not connection.EOF

你能帮我解决这个问题吗?

更新代码:

<html>
<body>
<%
Dim sConnString, connection, sSQL, backgroundColor
sSQL = "SELECT firstName, lastName, email, zip, country, company, industry, revenue, timestamp FROM users"
sConnString="a connection string for MS SQL database;"
Set connection = Server.CreateObject("ADODB.Connection")
Set rs = Server.CreateObject("ADODB.Recordset")
connection.Mode=adModeRead
rs.open sSQL,connection

Response.Write"<table>"
do while not connection.EOF
if(backgroundColor="#f1f1f1")then
backgroundColor="#ffffff"
else
backgroundColor="#f1f1f1"
end if

response.write "<tr backgroundColor="& backgroundColor & "></td>" & connection("firstName") & "</td><td>" & connection("lastName") & "</td><td>" & connection("email") & "</td><td>" & connection("zip") & "</td><td>" & connection("country") & "</td><td>" & connection("company") & "</td><td>" & connection("industry") & "</td><td>" & connection("revenue") & "</td><td>" & connection("timestamp") & "</td></tr>"

connection.MoveNext
Loop
Response.Write"</table>"

connection.Close
Set connection = Nothing
%>
</body>
</html>

【问题讨论】:

  • 您需要在其上创建记录集和 EOF - 而不是连接
  • 您能详细说明一下吗?我是 PHP 人,但出于某种原因,我不得不用 ASP 编写。

标签: asp-classic


【解决方案1】:

在您的代码中查看以下编辑 - 你创建一个记录集,然后打开它并执行它

<html>
<body>
<%
Dim sConnString, connection, sSQL, backgroundColor,objrs
sSQL = "SELECT firstName, lastName, email, zip, country, company, industry, revenue, timestamp FROM users"    
Set connection = Server.CreateObject("ADODB.Connection")
connection.connectionstring = "put your connection string in here"
' replace put your connection string in here with your connection string
Set objrs= Server.CreateObject("ADODB.Recordset")   
connection.Open()
objrs.open sSQL,connection

Response.Write"<table>"
do while not objrs.EOF
if(backgroundColor="#f1f1f1")then
backgroundColor="#ffffff"
else
backgroundColor="#f1f1f1"
end if

response.write"<tr backgroundColor="& backgroundColor & "></td>" & objrs("firstName") & "</td><td>" & objrs("lastName") & "</td><td>" & objrs("email") & "</td><td>" & objrs("zip") & "</td><td>" & objrs("country") & "</td><td>" & objrs("company") & "</td><td>" & objrs("industry") & "</td><td>" & objrs("revenue") & "</td><td>" & objrs("timestamp") & "</td></tr>"

objrs.MoveNext
Loop
Response.Write"</table>"
%>
</body>
</html>

注释:--

这需要类似于 - 不确定您使用的是什么数据库 -

 sConnString= "Provider=sqloledb;Data Source=ServerName;Initial Catalog=DatebaseName "

或者如果您有 dsn 连接,那么只需

sConnString = "DSN=xxxxx;uid=xxx;pwd=xxx"

【讨论】:

  • 我试过了,但是得到了以下错误:Microsoft VBScript runtime error '800a000d' Type mismatch /wsu.asp, line 21 Line 21: response.write"" & rs("firstName") & "" & rs("lastName") & " " & rs("email") & " td> " & rs("zip") & " " & rs("国家") & " " & rs("公司") & " " & rs("行业") & " " & rs("收入") & " " & rs("时间戳") & " ”在你的代码中,是不是假设是“objrs.open sSQL,connection”而不是“objrs.open sql,connection”?
  • @rdesai 是的,它应该是 ssql 。不要忘记将其标记为答案谢谢
  • ADODB.Recordset error '800a0e7d' 连接不能用于执行此操作。在这种情况下,它要么是关闭的,要么是无效的。 /testing/wsu.asp, line 10 Line 10: rs.open sSQL,connection sSQL = "SELECT firstName, lastName, email, zip, country, company, industry, income, timestamp FROM users" 我想知道怎么了。
  • 您是否在此行中添加任何内容 - 您必须将连接字符串替换为您的 sConnString='a 连接字符串
  • 我的连接字符串就像你提到的那样。我正在使用 MS SQL 数据库。请参阅问题中的更新代码。谢谢。
【解决方案2】:

您需要创建一个Recordset 来从您的 SQL 查询中读取数据。您已经使用了连接本身。

试试下面的例子。

HERE

【讨论】:

    猜你喜欢
    • 2016-01-27
    • 2020-03-25
    • 2015-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-30
    • 2015-10-06
    相关资源
    最近更新 更多