当前位置: 移动技术网 > IT编程>开发语言>Asp > asp实现读取数据库输出json代码

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

2017年12月08日  | 移动技术网IT编程  | 我要评论
复制代码 代码如下: function gettable(table,where,order,ordersort,curpage, pagesize,minijson)

复制代码 代码如下:

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开发的,可以无视

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

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网