经典ASP从SQL数据库中提取数据并将其放入数组中

时间:2017-01-13 14:26:50

标签: arrays asp-classic

一个简单的表(Table1),在SQL Server 2000中有两个字段(每月苹果的数量和两个季节的橘子数)。我需要使用经典ASP提取这些字段的值并填充两个数组。约束:这些数组已经被一个不受欢迎的应用程序定义,我不需要触及太多,因为我是经典ASP的初学者,我可能会做错事。

这些数组的定义如下:

Dim m_arrApples, m_arrOranges

我已成功连接到数据库,提取数据然后在浏览器中显示(格式图)。然而,当我需要在一个数组中传输它时(为了被那个不受欢迎的应用程序接管并处理它(以图形格式显示),我遇到了一个错误。这是我的代码:< / p>

'Open connection with SQL Server
Dim conn
Set conn = Server.CreateObject("ADODB.Connection")
conn.Open "Provider=SQLOLEDB;server=" & NameServer & ";database=" & DataBaseName & ";uid=" & NameUser & ";pwd=" & DataBasePassword & ";" 

'Open table and all records for further processing 
dim strSQL
strSQL = "select * from " & Table1
Set rst = conn.Execute(strSQL)

'Going through the records in the table and try to get values of each field and allocate them to the array
If rst.BOF And rst.EOF Then
    Response.Write "No data"
Else

i=0

Do While (Not rst.EOF)
i=i+1

'The line below confirms that the data extracted from database is displayed in the browser
'in its primary form (not graph), but that means I did it correctly up to this point 
Response.Write rst("Apples") & "/" & rst("Oranges") & "<br>"

m_arrApples(i)= rst("Apples")         ' THE ERROR IS AT THIS LINE. HERE THE SCRIPT IS STOPPED
m_arrOranges(i)= rst("Oranges")

rst.MoveNext
Loop
End If

错误是:

Microsoft VBScript runtime error '800a000d' 
Type mismatch 

任何提示都将受到高度赞赏。

2 个答案:

答案 0 :(得分:1)

使用动态数组时,您需要使用

ReDim Preserve m_arrApples(i + 1)

增加数组的大小,如果您已经知道大小使用固定数组(如果您知道有10个值使用Dim m_arrApples(9)

有用的链接

答案 1 :(得分:0)

尝试使用Recordset的Getrows()方法

'Create a Recordset
Dim objRS
Set objRS = Server.CreateObject("ADODB.Recordset")
objRS.Open "SELECT * FROM Table1", objConn

'now read the Recordset into a 2nd array
Dim aTable1Values
aTable1Values = objRS.GetRows()

然后你可以遍历2维数组:

Dim iRowLoop, iColLoop
For iRowLoop = 0 to UBound(aTable1Values, 2)
  For iColLoop = 0 to UBound(aTable1Values, 1)
    Response.Write(aTable1Values(iColLoop, iRowLoop) & "<br>")
  Next 'iColLoop

  Response.Write("<p>")
Next 'iRowLoop

此处提供更多信息 http://www.4guysfromrolla.com/aspfaqs/ShowFAQ.asp?FAQID=161