.

ASP读取数据库中数据到数组的类
分类:电脑知识 发表于:2011-01-19 22:15:54 评论(2)


<%

Class Class_Read

Public Arr,Rs,SQL,ArrTR,ArrTD,Page,TotalRead,TotalPage

Public Function Read(SQL,TD,TR,PG)

SQL = Replace(SQL,"’","")

Page= Int(PG)

Set Rs = Server.CreateObject("ADODB.Recordset") : Rs.open SQL,conn,1,1

TotalRead= Rs.RecordCount

If TotalRead>0 Then

If TR>0 Then : Rs.PageSize = TR : Else : TR=TotalRead

If TD>Rs.Fields.Count or TD<1 Then TD = Rs.Fields.Count

If TotalRead Mod TR <>0 Then TotalPage = 1 : End If : TotalPage = TotalPage + Int(TotalRead/TR)

If Page>=TotalPage Then Page=TotalPage : TR = TotalRead-TR*(TotalPage-1)

If Page>1 Then Rs.absolutePage=Page Else Page=1

End If

reDim Arr(TD-1,TR)

For ArrTR = 0 to TR-1 : If Rs.Eof Then Exit For

For ArrTD = 0 to TD-1 : Arr(ArrTD,ArrTR) = Rs(ArrTD) : Next : Rs.MoveNext

Next

ArrTR = ArrTR-1

Rs.Close

Set Rs=Nothing

End Function

End Class

Set C = New Class_Read

C.Read ("SQL语句","读取列数","读取行数[既每页显示条数]","当前页数")

Dim i

For i=0 To C.ArrTR

Response.Write "<br>内容:"&C.Arr(0,i)

Next

Response.Write "<br>总记录条数:"&C.TotalRead

Response.Write "<br>总页数 :"&C.TotalPage

Response.Write "<br>当前页 :"&C.Page

Response.Write "<br>当前记录数:"&C.ArrTR+1

%>

经过俺的简化及演变后,去掉列数和分页,并增加一个数组。

<%

Class Class_Read

Public Arr,Drr,Rs,SQL,ArrTR,TotalRead

Public Function Read(SQL,TotalRead)

SQL = Replace(SQL,"’","")

Set Rs = Server.CreateObject("ADODB.Recordset") : Rs.open SQL,conn,1,1

TotalRead= Rs.RecordCount

reDim Arr(TotalRead)

reDim Drr(TotalRead)

For ArrTR = 0 to TotalRead-1 : If Rs.Eof Then Exit For

Arr(ArrTR) = rs("字段1") :

Drr(ArrTR) = rs("字段2") :Rs.MoveNext

Next

ArrTR = ArrTR-1

Rs.Close

Set Rs=Nothing

End Function

End Class

Set C = New Class_Read

C.Read ("SQL语句","读取行数")

For i=0 To C.ArrTR

Response.Write "<br>内容:"&C.Arr(i) &C.Drr(i)

Next

%>

  
邮箱: 密码:
  • 简单生活 (2011-01-20 22:23:01)
    收藏起来慢慢看。嘿嘿 阿和发的东西肯定会用得上的~~ 简单生活,简单人生。
    • 阿和 博主 (2011-01-20 22:25:26)
      呃。。。俺是用上了。。不过不知道你喽。。。