正在加载...
2007-5
17
发表于: 未分类 | 作者: xurenlu
标签: ,

就是adodb.strem那段流传最广,很神奇呢。我那么辛苦写了个模板类,居然只有几个台湾人用,怪。

  1. <% 
  2.  
  3. ’************************************************************* 
  4. ’转发时请保留此声明信息,这段声明不并会影响你的速度! 
  5. ’************************************************************* 
  6.  
  7.  
  8. ’************************************************************* 
  9. ’@author:                        面条 
  10. ’@realname:                        徐仁禄 
  11. ’@email:                        xurenlu@sohu.com 
  12. ’@QQ:                            55547082 
  13. ’@Homepage:                        http://www.ksdn.net 
  14. ’@版权申明: 
  15. ’            非盈利性质团体或者个人可以免费使用. 
  16. ’************************************************************* 
  17. ’   我敢担保 本程序由本人独立完成 ,没有参考他人的任何程序(参考了本人自己的php版本的template,不过那个也是本人独立完成的 .)同时本人声明  本class的所有示例版权均为本人所有,任何人或者单位实体不得随意更改 
  18. ’ 本template可免费用于: 
  19. ’            1.个人的非商业性质应用。 
  20. ’            2.公益性质团体,如红十字会,孤儿院等等 
  21. ’   
  22. ’具体使用方法请看example.asp文件. 
  23. ’ 
  24. ’adSaveCreateOverWrite 
  25.  
  26. class template 
  27.     dim adSaveCreateOverWrite 
  28.     dim adSaveCreateNotExist 
  29.     public starttag 
  30.     public endtag 
  31.     public filename 
  32.     dim  key_arr() 
  33.     dim  val_arr() 
  34.     public content 
  35.     public total 
  36.     public contenta() 
  37.     public BlockContent ’ 块的内容(解析后的) 
  38.     public block_begin_delim 
  39.     public block_end_delim 
  40.     public block_begin_word 
  41.     public block_END_word 
  42.     public block_null 
  43.       
  44. sub Class_Initialize()’  类的初始化 
  45.  redim key_arr(0) 
  46.  redim val_arr(0) 
  47.  redim contenta(0) 
  48.  adSaveCreateOverWrite=2 
  49.  adSaveCreateNotExist=1 
  50.  
  51.  starttag="{" 
  52.  endtag="}" 
  53.  total=0 
  54.  block_begin_word="BEGIN" 
  55.  block_end_word="END" 
  56.  block_begin_delim="<!--" 
  57.  block_end_delim="-->" 
  58.  block_null=" "    ’  begin 和end之间用空格隔开 
  59. end sub 
  60.  
  61.  
  62. sub echo (a) 
  63. response.write a 
  64. end sub   
  65.  
  66.  
  67. function readfile(filepath) 
  68.     dim stm2 
  69.     on error resume next 
  70.     set stm2 =server.createobject("ADODB.Stream") 
  71.     stm2.Charset = "gb2312" 
  72.     stm2.Open 
  73.     stm2.LoadFromFile filepath 
  74.     readfile = stm2.ReadText 
  75. end function   
  76.  
  77.  
  78. function writefile(filepath,str)’ 写入文件的函数 
  79.     dim stm 
  80.     on error resume next 
  81.     Set stm = server.createobject("ADODB.Stream") 
  82.     stm.Charset = "gb2312" 
  83.     stm.Open 
  84.     stm.WriteText str 
  85.     stm.SaveToFile filepath, adSaveCreateOverWrite 
  86. end function 
  87.  
  88. function SetFile(file)’ 设置文件,读取文件内容 
  89.     filename=file 
  90.     content=readfile(file) 
  91. end function   
  92.  
  93. function inarray(val,arr)’val是否在数组arr中 
  94.     dim tmp,i,rr,re,pt,tt 
  95.     for i =0 to ubound(arr) 
  96.     if arr(i)=val then 
  97.         inarray=i 
  98.         exit function     
  99.     end if 
  100.     next 
  101.     inarray=-1 ’不在数组中. 
  102. end function   
  103.  
  104. function listarray(arr,str) 
  105.         dim tmp,i,rr,re,pt,tt 
  106.         str="   " & str 
  107.       
  108.         for i=0 to ubound(arr) 
  109.             echo str & i & ":" & arr(i) & vbcrlf   
  110.         next 
  111. end function   
  112.  
  113. function NewKey(key,val) ’添加新的键值. 
  114.       dim tmp,i,rr,re,pt,tt,pos 
  115.     i=total   
  116.     pos=inarray(key,key_arr) 
  117.       
  118.     if  pos=-1 then  ’//如果这个键值不存在. 
  119.           
  120.      redim Preserve key_arr(i) 
  121.      redim Preserve val_arr(i) 
  122.           
  123.           
  124.         ’echo "key_arr(" & i & ")=" & key & vbcrlf 
  125.           
  126.         key_arr(i)=key 
  127.         val_arr(i)=val 
  128.         total=total+1 
  129.     else 
  130.         key_arr(pos)=key 
  131.         val_arr(pos)=val 
  132.     end if   
  133. end function   
  134.  
  135.  
  136. function resetKeys()’ 初始化键名数组 
  137.     redim key_arr(0) 
  138.     redim val_arr(0) 
  139.     total=0 
  140. end function   
  141.  
  142.  
  143. function getTextContent(Tcontent) 
  144.     dim tmp,i,rr,re,pt,tt 
  145. ’ 得到把某一个文本段的{}内容替换后的块. 
  146.     tmp=Tcontent 
  147.     for i=0 to total -1 
  148.         tmp=replace(tmp & "",starttag & key_arr(i) & endtag, val_arr(i)& "" )  ’ 替换各个键值. 
  149.     next 
  150.     ’  替换{***}类似的东西。 
  151.     ’  目前暂时先放一放把。 
  152.       
  153.     ’’ 
  154.     ’’ 
  155.     set re=new RegExp 
  156.     re.Global=True 
  157.     re.Ignorecase=True 
  158.     pt="{([a-zA-Z0-9_]{0,100})}" 
  159.     re.Pattern=pt 
  160.     set tt=re.Execute(tmp) 
  161.       
  162.       
  163.     for i= 0 to tt.count -1 
  164.     tmp=replace(tmp & " ", tt.item(i) & "" ,"") 
  165.     next 
  166.     set re=nothing 
  167.     set tt=nothing 
  168.     ’’ 
  169.     ’’ 
  170.       
  171.     getTextContent=tmp 
  172. end function   
  173.  
  174.  
  175. function getText() 
  176.     dim tmp,i,rr,re,pt,tt 
  177. ’ 得到把某一个文本段的{}内容替换后的块. 
  178.     tmp=content 
  179.     for i=0 to total -1 
  180.         tmp=replace(tmp & "",starttag & key_arr(i) & endtag & "", val_arr(i) & "" )  ’ 替换各个键值. 
  181.     next 
  182.     ’  替换{***}类似的东西。 
  183.     ’  目前暂时先放一放把。 
  184.       
  185.     ’’ 
  186.     ’’ 
  187.     set re=new RegExp   ’ 这里是模式匹配的应用  有正规表达式应用高手的指导一下! 
  188.     re.Global=True 
  189.     re.Ignorecase=True 
  190.     pt="{([a-zA-Z0-9_]{0,100})}" 
  191.     re.Pattern=pt 
  192.     set tt=re.Execute(tmp) 
  193.     for i= 0 to tt.count -1 
  194.     tmp=replace(tmp & "", tt.item(i) & "","") 
  195.     next 
  196.     set re=nothing 
  197.     set tt=nothing 
  198.     ’’ 
  199.     ’’ 
  200.       
  201.     getText=tmp 
  202.     content=tmp 
  203. end function   
  204.  
  205.  
  206. function getBlockContent(block)’ 得到模板内容中某一个块的内容 
  207.     dim i,pos1,pos2,firststr,secondstr,tempstr 
  208.     firstStr="<!-- BEGIN " & Block & " -->" 
  209.     secondStr="<!-- END " & Block & " -->" 
  210.     pos1=instr(content,firststr) 
  211.     pos2=instr(content,secondstr) 
  212.     
  213.     if  (pos2-pos1)<=0 then 
  214.       
  215.     else 
  216.  
  217.         tempstr=mid(content,pos1,pos2-pos1) 
  218.         tempstr=replace(tempstr,firststr,"") 
  219.         tempstr=replace(tempstr,secondstr,"") 
  220.                 ’response.write replace(tmpstr,"<--","") 
  221.     end if 
  222.     ’response.end 
  223.       
  224.     getBlockContent=tempstr ’ 返回该字符串. 
  225. end function   
  226.  
  227.  
  228. sub tofile(file)’ 输出到某个文件 
  229.     dim tmp 
  230.     tmp=gettext() 
  231.     writefile file,content’ 输出到文件 
  232. end sub 
  233.  
  234.  
  235. function ParseBlock(block) ’ 到到某一个块的解析后的内容. 
  236.     dim b,tmp 
  237.     dim firststr,secondstr,tempstr 
  238.     b=GetBlockContent(block) ’得到某一个块解析前的内容 
  239.     tmp=getTextContent(b)’得到这个块解析后的内容. 
  240.     BlockContent=BlockContent & tmp  ’ 保存起来拉 哈哈。这样就实现了重复显示某一个块. 
  241.     firstStr="<!-- BEGIN " & Block & " -->" 
  242.     secondStr="<!-- END " & Block & " -->" 
  243.     tmp=replace(tmp,firststr,"") 
  244.     tmp=replace(tmp,secondstr,"") 
  245.     ParseBlock=tmp   
  246. end function   
  247.  
  248. function replaceBlock(block)’ 把解析了几次的块的内容给替换解析了. 
  249.     dim con,tmp 
  250.         dim firststr,secondstr,tempstr 
  251.           
  252.     con=GetBlockContent(block) ’得到这个块解析前的内容. 
  253.     tmp=replace(content & "",con & "",Blockcontent & "") 
  254.     blockcontent="" 
  255.       
  256.     firstStr="<!-- BEGIN " & Block & " -->" 
  257.     secondStr="<!-- END " & Block & " -->" 
  258.       
  259.     tmp=replace(tmp,firststr,"") 
  260.     tmp=replace(tmp,secondstr,"") 
  261.       
  262.     content=tmp   
  263. end function   
  264.  
  265. function replaceBlockforNUll(block)’ 把解析了几次的块的内容给替换解析了. 
  266.     dim tmp,con 
  267.     con=GetBlockContent(block) ’得到这个块解析前的内容. 
  268.     tmp=replace(content & "",con & "","") 
  269.     blockcontent="" 
  270.     content=tmp   
  271. end function   
  272.  
  273. function replaceBlockfor(block,deStr)’ 把解析了几次的块的内容给替换解析了. 
  274.     dim tmp,con 
  275.     con=GetBlockContent(block) ’得到这个块解析前的内容. 
  276.     tmp=replace(content & "",con & "",Dstr) 
  277.     blockcontent="" 
  278.     content=tmp   
  279. end function   
  280. end class 
  281.  
  282.  
  283. %> 
  284.  
  285.  
  286. 2、数据库结果集合操作类
  287.  
  288. 程序代码 程序代码
  289. <% 
  290.  
  291. ’************************************************************* 
  292. ’转发时请保留此声明信息,这段声明不并会影响你的速度! 
  293. ’************************************************************* 
  294.  
  295.  
  296. ’************************************************************* 
  297. ’@author:                        面条 
  298. ’@realname:                        徐仁禄 
  299. ’@email:                        xurenlu@sohu.com 
  300. ’@QQ:                            55547082 
  301. ’@Homepage:                        http://www.ksdn.net 
  302. ’@版权申明: 
  303. ’            非盈利性质团体或者个人可以免费使用. 
  304. ’************************************************************* 
  305.  
  306.  
  307. ’************************************************************* 
  308. ’************************************************************* 
  309. ’************************************************************* 
  310. class Rsclass 
  311.     private iRs 
  312.     private isql 
  313.     private iconn 
  314.     private closeConn 
  315.     private openConn 
  316.  
  317. ’************************************************************* 
  318. ’        进行初始化,建立iconn和irs对象. 
  319. ’************************************************************* 
  320.     Private Sub Class_initialize()   
  321.         set iconn=server.createobject("adodb.connection") 
  322.         set irs=server.createobject("adodb.recordset") 
  323.     End Sub   
  324.       
  325.     public sub connect(connstr) 
  326.         err.clear 
  327.         on error resume next 
  328.         iconn.open connstr 
  329.         if err.number<>0 then response.write "数据联接出错了." 
  330.     end sub 
  331.       
  332.     public property let conn(connection) 
  333.         set iconn=connection        ’通过外界传入一个数据库联接. 
  334.     end property 
  335.       
  336.       
  337.       
  338.     Private Sub Class_Terminate()   
  339.         on error resume next 
  340.         irs.close 
  341.         iconn.close 
  342.         set iconn=nothing 
  343.         set irs=nothing 
  344.     End Sub   
  345.  
  346. ’************************************************************* 
  347. ’        内容:实现统计功能,一般用select count(*) from table where id>54类似的sql语句. 
  348. ’************************************************************* 
  349.     public function getCountBysql(sql) 
  350.         on error resume next 
  351.         dim count 
  352.         count=iconn.execute(sql)(0) 
  353.         getCountBysql=count 
  354.         iconn.close 
  355.     end function   
  356.       
  357.       
  358.       
  359.  
  360. ’************************************************************* 
  361.     public function deleteBySql(sql) 
  362.         err.clear 
  363.         on error resume next 
  364.         iconn.execute(sql) 
  365.         if err.errcode<>0 then 
  366.             deleteBysql=false 
  367.             exit function 
  368.         end if 
  369.         deleteBySql=true 
  370.     end function   
  371.  
  372.  
  373. ’************************************************************* 
  374.     public function deleteByid(table,column,value) 
  375.         err.clear 
  376.         on error resume next 
  377.         iconn.execute("delete from "&table & " where "&column & "=’"&value & "’") 
  378.         if err.errcode<>0 then 
  379.             deleteByid=false 
  380.             exit function 
  381.         end if 
  382.         deleteByid=true 
  383.     end function   
  384.  
  385.  
  386. ’************************************************************* 
  387. ’        返回一个词典,用这个词典来装载要添加的对象. 
  388.     public function getdict() 
  389.         set getdict=server.createobject("scripting.dictionary")     
  390.     end function   
  391.  
  392. ’************************************************************* 
  393. ’        增加新的记录. 
  394.     public function add(obj,table) 
  395.         dim item 
  396.         isql="select * from " & table 
  397.         response.write isql 
  398.         iRs.open isql,iconn,1,3 
  399.         irs.addnew 
  400.         for each item in obj 
  401.             irs(item)=obj(item) 
  402.         next   
  403.         irs.update 
  404.         irs.close 
  405.     end function   
  406.       
  407.       
  408.  
  409. ’************************************************************* 
  410. ’        得到查询得返回值 
  411.     public function getquery(sql) 
  412.         iRs.open sql,iconn,1,1   
  413.         if irs.eof and irs.bof then 
  414.             getquery=null 
  415.         else 
  416.             set getquery=irs 
  417.         end if 
  418.     end function   
  419.  
  420.     public function getqueryPage(sql,pageNum,PageSize) 
  421.         on error resume next 
  422.         iRs.pageSize=pageSize 
  423.         iRs.open sql,iconn,1,1   
  424.         iRs.AbsolutePage=pageNum 
  425.         if irs.eof and irs.bof then 
  426.             getqueryPage=null 
  427.         else 
  428.             set getqueryPage=irs 
  429.         end if 
  430.           
  431.         response.write "<span style=’color:green;’>"& err.number & err.description &"</span>" 
  432.     end function   
  433.       
  434.       
  435. ’************************************************************* 
  436. ’        让结果集移动到第n页. 
  437.     public function setPage(pageNum,PageSize) 
  438.         on error resume next 
  439.         iRs.moveFirst 
  440.         iRs.move((pageNum-1)*PageSize) 
  441.     end function   
  442.  
  443.  
  444. end class 
  445.  
  446.  
  447. ’************************************************************* 
  448. 用法示例: 
  449. dim cstr 
  450. dim path 
  451. path=server.mappath("2data.mdb") 
  452. cstr= "DBQ="+path+";DefaultDir=;DRIVER={ Microsoft Access Driver (*.mdb)};" 
  453. dim rec 
  454. set rec=new RsClass 
  455. rec.connect(cstr) 
  456.  
  457.  
  458. response.write "************************" 
  459. set tt=rec.getqueryPage("select top 8 * from help",2,4) 
  460. response.write tt.recordcount & "<br>" 
  461. do while not tt.eof   
  462.     response.write tt(0) &"<br>" 
  463.     tt.movenext 
  464. loop 
  465. tt.movefirst 
  466. tt.close 
  467. %> 
  468.  
  469.  
  470.  
  471. 数据库操作类
  472.  
  473. 程序代码 程序代码
  474. <% 
  475. ’────────────────────────────────   
  476. ’功能说明:db类是实现数据库连接的类,里面留有数据库连接字符串接口 
  477. ’包括模块:无,一般都是被其他模块包括 
  478. ’调用方法:1、如果使用原有数据库连接,则不用更改数据库连接字符串ConnStr 
  479. ’             具体操作为:Set DBC=New DataBaseClass 
  480. ’                         DBC.ConnStr="其他连接字符串" 
  481. ’          2、方法使用:Set Conn=DBC.OpenConnection()得到一个连接对象 
  482. ’────────────────────────────────   
  483. Class dbclass 
  484. ’────────────────────────────────   
  485. ’定义变量   
  486. Private IConnStr   
  487. ’────────────────────────────────   
  488. ’ ConnStr属性 
  489. Public Property Let ConnStr(Val) 
  490.     IConnStr = Val 
  491. End Property 
  492. ’────────────────────────────────   
  493. ’ ConnStr属性   
  494. Public Property Get ConnStr() 
  495.     ConnStr = IConnStr 
  496. End Property 
  497. ’────────────────────────────────   
  498. ’ 类初始化   
  499. Private Sub Class_initialize()   
  500. End Sub   
  501. ’────────────────────────────────   
  502. ’ 类注销   
  503. Private Sub Class_Terminate()   
  504.     ConnStr = Null   
  505. End Sub   
  506. ’────────────────────────────────   
  507. ’ 建立一个连接   
  508. Public Function OpenConnection()   
  509.     Dim TempConn 
  510.     ’On Error Resume Next 
  511.     Set TempConn = Server.CreateObject("ADODB.Connection") 
  512.     TempConn.Open ConnStr   
  513.     Set OpenConnection = TempConn   
  514.     Set TempConn = Nothing   
  515.     if Err.Number <> 0 then 
  516.         Response.Write ("<script>alert(’[系统错误]\n\n数据库连接错误!请检查系统参数设置>>站点常量设置,或者 /inc/const.asp文件!’);</script>")   
  517.         Response.End 
  518.     end if 
  519. End Function   
  520. End Class 
  521.  
  522. %> 
  523.  
  524. 基于adodb.stream的文件操作类
  525.  
  526. 程序代码 程序代码
  527. <% 
  528.  
  529. ’************************************************************* 
  530. ’转发时请保留此声明信息,这段声明不并会影响你的速度! 
  531. ’************************************************************* 
  532.  
  533.  
  534. ’************************************************************* 
  535. ’@author:                        面条 
  536. ’@realname:                        徐仁禄 
  537. ’@email:                        xurenlu@sohu.com 
  538. ’@QQ:                            55547082 
  539. ’@Homepage:                        http://www.ksdn.net 
  540. ’@版权申明: 
  541. ’            非盈利性质团体或者个人可以免费使用. 
  542. ’************************************************************* 
  543.  
  544.  
  545. ’************************************************************* 
  546. ’        类名称:        files 
  547. ’        类功能:            实现文件读写功能,利用adodb.stream实现,在不支持fso的主机上也可以读写文件. 
  548. ’************************************************************* 
  549.  
  550. class files 
  551.       
  552.       
  553.     private adSaveCreateOverWrite    ’创建文件的时候可以覆盖已经存在的文件. 
  554.     private adSaveCreateNotExist    ’保存文件的时候如果文件不存在,可以创建文件. 
  555.  
  556.  
  557. ’************************************************************* 
  558. ’        事件名称:        Class_Initialize() 
  559. ’        事件发生条件:    类创建时候产生该事件 
  560. ’        事件内容:        给私有变量赋值 
  561. ’        事件传入参数:    无 
  562. ’************************************************************* 
  563.  
  564.  
  565.     sub Class_Initialize() 
  566.          adSaveCreateOverWrite =2 
  567.          adSaveCreateNotExist = 1 
  568.     end sub 
  569.  
  570.  
  571. ’************************************************************* 
  572. ’        函数名称:        function readfile(filepath) 
  573. ’        函数内容:        读出文件 
  574. ’        传入参数:        filepath:要读的文件的绝对路径 
  575. ’        返回参数:        要读的文件的内容. 
  576. ’************************************************************* 
  577. function readfile(filepath) 
  578.       
  579.       
  580.     on error resume next 
  581.       
  582.     dim stm2 
  583.       
  584.       
  585.     set stm2 =server.createobject("ADODB.Stream") 
  586.     stm2.Charset = "gb2312" 
  587.     stm2.Open 
  588.     stm2.LoadFromFile filepath 
  589.     readfile = stm2.ReadText 
  590. end function   
  591.  
  592.  
  593. ’************************************************************* 
  594. ’        函数名称:        function writefile(filepath,str) 
  595. ’        函数内容:        写入文件 
  596. ’        传入参数:        filepath:要读的文件的绝对路径 
  597. ’                        str:    要写入的内容 
  598. ’        返回参数:        无返回 
  599. ’************************************************************* 
  600.  
  601.  
  602.     function writefile(filepath,str) 
  603.  
  604.  
  605.         on error resume next 
  606.           
  607.           
  608.         Set stm = server.createobject("ADODB.Stream") 
  609.         stm.Charset = "gb2312" 
  610.         stm.Open 
  611.         stm.WriteText str 
  612.         stm.SaveToFile filepath, adSaveCreateOverWrite 
  613.     end function 
  614.       
  615.       
  616.  
  617. ’************************************************************* 
  618. ’        函数名称:        function copy(filepath_s,filepath_d) 
  619. ’        函数内容:        读出文件 
  620. ’        传入参数:        filepath_d:目的文件的绝对路径   
  621. ’                        filepath_s:源文件路径 
  622. ’************************************************************* 
  623. function copy(filepath_s,filepath_d)     
  624.     on error resume next     
  625.     dim stm2 
  626.     set stm2 =server.createobject("ADODB.Stream") 
  627.     stm2.Charset = "gb2312" 
  628.     stm2.Open 
  629.     stm2.LoadFromFile filepath_s 
  630.     stm2.SaveToFile filepath_d, adSaveCreateOverWrite 
  631. end function   
  632.  
  633.  
  634. end class 
  635.  
  636.  
  637. %>

: http://www.162cm.com/archives/408.html

本文相关评论 - 才一条评论
tgw2000
2008-06-03 10:05:39

具体使用方法请看example.asp文件,你的example.asp文件在哪里呢?