网络编程 
首页 > 网络编程 > 浏览文章

asp实现读取数据库输出json代码

(编辑:jimmy 日期: 2024/11/14 浏览:3 次 )

复制代码 代码如下:
Function GetTable(Table,Where,Order,OrderSort,curpage, pagesize,MiniJson)
'Author : nigou
'使用方法 : Response.Write GetTable(Table表名,Where条件,Order主键(必须),OrderSort(asc,desc),curpage当前页, pagesize每页条数,MiniJson是否输出为miniUI格式)
'
    dim i, j ,rs
    if OrderSort="" Then OrderSort="asc"
    if Where="" Then Where="asc"
    Set Rs=Server.CreateObject("adodb.recordset")
    if curpage>1 then
        TmpSql="select a.* from ( select top  " & (curpage) * pagesize & " * from " & Table & " where " & where  & " order by " & Order & " " & OrderSort & ") a left join ( select top " & (curpage-1) * pagesize  & " * from " & Table & "  where " & where  & " order by " & Order & " " & OrderSort & ") b on a." & Order & "=b." & Order & " where iif(b." & Order & ",'0','1')='1'"
    else
        TmpSql="select a.* from ( select top  " & (curpage) * pagesize & " * from " & Table & " where " & where  & " order by " & Order & " " & OrderSort & ") a "
    end if
    if pagesize=0 Then TmpSql = "select * from " & Table
        Rs.open TmpSql,conn,1,1
        RealSize=Rs.recordcount
        for i=0 to Rs.recordcount-1
            TmpJson= TmpJson & "{"
            for j=0 to Rs.fields.count-1
                TmpJson= TmpJson & """" &(Rs.fields(j).name) & """ : "            
                TmpJson= TmpJson & """" & Rs(j) & """"
                if j<Rs.fields.count-1 then TmpJson= TmpJson & " , "
            next
            TmpJson= TmpJson & " }"
            if i<Rs.recordcount-1 then TmpJson= TmpJson & ","
            TmpJson= TmpJson & vbcrlf
            rs.movenext
        next
        Rs.close
    if MiniJson=1 Then
        CountSql="select count("& order &") from " & Table & " where " & where
        Rs.open CountSql,Conn,1,1
        Counts=Rs(0)
        Rs.Close
        GetTable=ToMiniUi(TmpJson,Counts)
    Else
        GetTable=toJson(TmpJson)
    end if
    Set Rs=nothing
End Function
function toJson(JsonStr)
    toJson="[" & vbcrlf & JsonStr & vbcrlf & "]"
end function
Function ToMiniUi(JsonStr,total)
    ToMiniUI="{"
    ToMiniUI=ToMiniUI & """total"":"" " & total & """," & vbcrlf
    ToMiniUI=ToMiniUI & """data"": [" & JsonStr
    ToMiniUI=ToMiniUI & "]}"
End Function

PS:最后的参数是针对miniUI开发的,可以无视

以上就是本文的全部内容了,希望大家能够喜欢。

上一篇:asp实现获取MSSQL数据库表指定条件行数的函数
下一篇:JScript中遍历Request表单参数集合的方法
一句话新闻
高通与谷歌联手!首款骁龙PC优化Chrome浏览器发布
高通和谷歌日前宣布,推出首次面向搭载骁龙的Windows PC的优化版Chrome浏览器。
在对骁龙X Elite参考设计的初步测试中,全新的Chrome浏览器在Speedometer 2.1基准测试中实现了显著的性能提升。
预计在2024年年中之前,搭载骁龙X Elite计算平台的PC将面世。该浏览器的提前问世,有助于骁龙PC问世就获得满血表现。
谷歌高级副总裁Hiroshi Lockheimer表示,此次与高通的合作将有助于确保Chrome用户在当前ARM兼容的PC上获得最佳的浏览体验。