繁体   English   中英

经典ASP将数组传递给函数

[英]Classic ASP passing array to a function

在经典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

%>

任何提示将不胜感激。

使用.GetRows获取表/结果集的数组。 要将这样的数组传递给函数(为什么是函数?其返回值应该是什么?),请将其名称写在函数调用的参数列表中。

更新wrt评论:

调用期望数组的函数的示例:

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

您可以在有效的记录集上使用.GetRows(),而不是返回一维数组的Split()(请记住,.GetRows()返回二维数组)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM