2012-04-23 12 views
0

執行查詢以下是我的工作的ASP代碼:無法在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 error '800a0bb9'

Arguments are of the wrong type, are out of acceptable range, or are in conflict with one another.

/wsu.asp, line 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> 
+0

你需要創建一個記錄和EOF - 而不是連接 – DaveHogan 2012-04-23 10:51:19

+0

能否請您詳細說說嗎?我是一個PHP的人,但我不得不在ASP中寫這個出於某種原因。 – 2012-04-23 10:57:00

回答

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" 
+0

我試過了,但得到了以下錯誤: Microsoft VBScript運行時錯誤 '800a000d' 類型不匹配 /wsu.asp,行21 線21: 的Response.Write 「」 &RS( 「名字」)& 「​​」 &RS( 「姓氏」)& 「​​」 &RS( 「電子郵件」)& 「​​」 &RS( 「拉鍊」)&「​​「和RS(」 國家 「)和」​​「和RS(」 公司 「)及」​​「&RS(」 工業 「)與」​​「和RS(」 收入 「)」​​「和RS(」 時間戳 「)和」「 而在你的代碼,是猜想是「objrs.open sSQL,連接」而不是「objrs.open sql,連接」? – 2012-04-23 11:19:26

+0

@rdesai,是的改變,它應該工作。 – 2012-04-23 12:03:27

+0

@rdesai是的,它應該是ssql。不要忘記標記爲答案謝謝 – Jatin 2012-04-23 12:07:47

0

您將需要創建一個Recordset從您的SQL查詢讀取數據。你已經使用連接本身。

試試下面的例子。

HERE