无法在ASP中执行查询

时间:2012-04-23 10:48:35

标签: asp-classic

以下是我正在处理的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>

2 个答案:

答案 0 :(得分: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"

答案 1 :(得分:0)

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

尝试以下示例。

HERE