蓝森林首页 | 返回主页 | 本站地图 | 站内搜索 | 联系信箱 |
 您目前的位置:首页 > 自由软件 > 技术交流 > 应用编程


    

蓝森林 http://www.lslnet.com 2006年8月25日 8:28

[asp]一个求记录数的函数,有点问题!

<%
                function getcount(tablename)
                    dim rs,cb
                    set db=server.createobject("ADODB.Connection")
                    connstr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & server.mappath("data/msmaxblog.mdb")
                    db.open connstr
                    set rs=server.CreateObject("adodb.recordset")
                    [color=Red]strsql="select * from " & tablename[/color]
                    rs.open strsql,db,1
                    countArticle=rs.recordcount
                    getcount=countArticle
                end function
            %>
                博文总数:<%response.write     getcount(article%>
                评论总数:<%response.write  getcount(re)%>
                留言总数:<%response.write  getcount(message)%>
                计划总数:<%response.write  getcount(plan)%>
                完成计划:<%response.write  getcount(planed)%>



就是那段strsql有点问题!不晓得怎么写!

strsql="select * from " &tablename&" "

strsql="select * from [" & tablename & "]"

只是查询一个数目,没必要那么耗费资源的 不需要用rs 也不要每次写打开连接的,更不要 select *

set db=server.createobject("ADODB.Connection")
        connstr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & server.mappath("data/msmaxblog.mdb")
       db.open connstr

function GetNum(stable)
    GetNum=db.execute("select count(1) from ["&stable&"]")(0)
end function


[color=Red]错误类型:
Microsoft VBScript 编译器错误 (0x800A0408)
无效字符
/msmax/count.asp, line 17
  sqlstr="select count(1) from ["&stable&" ]"[/color]



还是要报错。记得好像要用单引号哇!

用了你的方法,但是他不显示呢?



多了一个空格。。。

GetNum=db.execute("select count(1) from ["&stable&"]")(0)


你调用方法错了。。
调用:

评论总数:<%response.write  getnum("re")%> 加引号呢  


师兄,可不可以再检查一下,还有没得错,照你方法。放上去,我幼小的心灵又一次受打击了。我把代码全奉上了,这样就感觉是对的!但就是不显示。



        [color=Blue]set db=server.createobject("ADODB.Connection")
                connstr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & server.mappath("data/msmaxblog.mdb")
               db.open connstr
                function GetNum(stable)
                           GetNum=db.execute("select count(1) from ["&stable&"]")(0)
                end function
                        %>
                                博文总数:<%response.Write GetNum(article)%><br/>
                                评论总数:<%response.write  GetNum(re)%><br/>
                                留言总数:<%response.write  GetNum(message)%><br/>
                                计划总数:<%response.write         GetNum(plan)%><br/>
                                完成计划:<%response.write         GetNum(planed)%><br/>[/color]

完成计划:<%response.write        GetNum(planed)%>

给你说了 调用可能错了 表名加引号  改成:

完成计划:<%response.write        GetNum("planed")%>

我这边测试过 一直用这个

现在行了,谢谢[color=Red]wyq29 [/color],这么耐心的回复。小生感激不尽!




Copyright © 1999-2000 LSLNET.COM. All rights reserved. 蓝森林网站 版权所有。 E-mail : webmaster@lslnet.com