经典ASP将数组传递给函数

时间:2014-06-16 19:33:16

标签: arrays vbscript asp-classic

在经典ASP中,我需要从MSSQL数据库中提取数据,将结果传递给二维数组(行,列)并以各种格式显示数据。

对于每种这样的格式,我需要构建显示数据的函数。因此,为了尽可能模块化,我需要分离(i)从(ii)显示结果中提取和传递数据到数组。

我的代码目前使用类提取数据,但也以原始方式显示(在同一个类中)数据(只是为了测试数据的提取和纠正)。 如何将这样的数组传递给函数?您可以想象将数组作为数据输入到函数中然后在尝试将其显示在表中时对其进行操作(创建许多函数)是多么灵活(例如:函数1将基于表的模板编号1)红色,背景为黑色,没有边框,功能2构建在模板2上,表格为绿色,边框和黄色背景等等。)

这是我的代码,在主函数的末尾(在类中),您将看到一个显示结果的部分,即我需要在类之外/之外单独执行的部分(即在函数中被创造。)

<!--#include file ="../functions/fctGetnrofrecords.asp"-->
<%

Dim db : Set db = New GetRowsFromAdatabase
db.strTable="Customers"
strDisplay=db.FindOutRecordsAndPassThemToAnArray()

Response.Write strDisplay

Class GetRowsFromAdatabase

Public strTable
Public numberOfRecords
Public numberOfFields

Public Function FindOutRecordsAndPassThemToAnArray()

Set conn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")

'Find out connecting credentials
  strSERVERspecific=Coli(0)
  strDATABASENAMEspecific=Coli(1)
  strUIDspecific=Coli(2)
  strPWDspecific=Coli(3)

conn.Open "Provider=SQLOLEDB;server=" & strSERVERspecific & ";database=" & strDATABASENAMEspecific & ";uid=" & strUIDspecific & ";pwd=" & strPWDspecific & ";"

rs.Open strTable, conn

            if rs.EOF and rs.BOF then
                    strError = "There is no record in the table " & strTable
            else
                    'Assign the Number Of Fields to the variable “counter”
                    counter = rs.fields.count
                   numberOfFields=counter

                    Dim matrix(25, 10) ' these exceed by far the values of numberOfRecords and numberOfFields

                    for j=0 to counter-1
                         matrix(0,j)= rs.Fields(j).Name ' The first dimension of the array, when is zero,
                                                        ' is populated with the names of fields
                    next

                    rs.movefirst
                    i=1
                    do until rs.EOF

                            for j=0 to counter-1
                                    matrix(i,j)=rs(j)
                            next
                    i=i+1
                    rs.movenext
                    loop
            end if

' Now, I need this class not to include the displaying section that follows
' (i.e. see the portion until the end of this function), although this section works fine

numberOfRecords=fctGetNumberOfRowsOfaTable(strTable) 
'see the include directive at the beginning of this code (there is a function there that does this)

'====begin section that displays the arrays values
for m = 0 to numberOfRecords
  for n=0 to counter-1
         strDisplay = strDisplay & m & "," & n & "=" & matrix(m,n) & "<br>"
  next
next
'====end section that displays the array values

FindOutRecordsAndPassThemToAnArray = strDisplay

End Function



Public Function Coli(x)

'This function read a line of a txt file located on the server side (hidden from public / httpdocs)

' where x =  the relevant line out of the following
                    ' 1 means the 1st line = name / IP server
                    ' 2 means the 2nd line = database name
                    ' 3 means the 3rd line = user name available in the database
                    ' 4 means the 4th line = user’s password

if x<0 or x> 3 then
  Coli="Erorr"
  Exit Function
else

serv=Server.MapPath("../../../")
path=serv & "\somehiddenfolder\"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile(path & "configuration.txt")

J=0
Dim Datele(3)
   Do Until objTextFile.AtEndOfStream
   strNextLine = objTextFile.Readline

   if x=J then
      Coli=strNextLine
      exit function
   else
      J=J+1
   end if
Loop
end if
End Function

End Class

%>

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

1 个答案:

答案 0 :(得分:0)

使用.GetRows获取表/结果集的数组。要将这样的数组传递给函数(为什么函数?它的返回值是什么?),你可以在函数调用的参数列表中写出它的名字。

更新评论:

调用需要数组的函数的示例:

>> Function sum(a)
>>   sum = Join(a, "+")
>> End Function
>> a = Split("1 2 3")
>> WScript.Echo sum(a)
>>
1+2+3

而不是返回一维数组的Split() - 你在有效的记录集上使用.GetRows()(并记住.GetRows()返回一个二维数组。)