asp(vbscript)数据库操作类

对实现最基本的asp对数据库的读、写、删、改都十分的方便,具体使用方法,懒得写,大家自己研究一下, 随便在这里写一个实例:
<%
set datas =new data '实例化类
datas.db ="data.mdb" '设定数据库路径
datas.stype=1 '设定数据库连接方式,1为acc,2为sql
datas.Datebase '连接数据库
datas.page_size=20 '每页显示20条
datas.Record "","admin","","id DESC",1,true '建立记录集,第一个参数是要返回的字段,用逗号分开;第二个参数是要返回的数据表,第三个参数是返回的比对条件,第四个参数是排序,第五个参数是读取数 据库的方式,最后一个参数是分页开关,true是进行分页显示,false则不进行分页
for 1=1 to datas.x '从1开始循环,一直到当前页的记录总数
if datas.ps(false) then exit for '如果返回eof则跳过循环
%>
<%=datas.show("id")%><!--返回id字段的值-->
<%
datas.nex '移动数据库指针
next
%>
<%
datas.pagehtml "<li><a href='$1' class='$2'>$3</a></li>","<li><span class='ms'>$1</span></li>","<li><samp class='gong'>$1</samp></li>" '显示分页导航,第一个参数是分页连接的html元素及样式,第二个参数是非导航连接的html元素,第三个参数是导航说明的html元素
%>
<%
datas.ul '关闭及释放记录集
datas.cn '关闭及释放数据库连接
Set datas=nothing '释放类
%>

<%
'数据库操作类
'asp 版本  gb2312
'制作:伤心的歌 QQ:369758482  url:http://www.q128.net

'On Error Resume Next

class data

  public sub w(vals)
   Response.Write(Replace(vals,"\n",vbcrlf))
  end sub

  public sub memsg(txt,urls)
   if urls="" then urls="javascript:history.back();"
   if txt<>"" then
    Response.Write("<script type='text/javascript'>alert('"&txt&"');document.location='
"&urls&"';</script>")
   else
    Response.Redirect(urls)
   end if
	Response.End()
  end sub

  public db,loca,locauser,locapass,locadata,connstr,conn,rs,stype,sql,page_size,pagenum

  public Sub Database()
     if stype=1 then
	  connstr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath(db) & ""
	 else
	  connstr="Provider=SQLOLEDB.1;Password='"&locapass&"';Persist Security Info=True;User
ID='"&locauser&"';Initial Catalog='"&locadata&"';Data Source='"&loca&"'"
	 end if


	    Set conn=Server.CreateObject("ADODB.Connection")
	    conn.open connstr
      if err then
	   Response.Write("数据库连接错误,请检查数据库参数")
	   Response.End()
	  end if

  end Sub



  public Sub Record(fielder,table,Condition,sorts,ty,fenye)
   if fielder="" then fielder="*"
     sql="select "&fielder&" from "&table
   if Condition<>"" then sql=sql&" where "&Condition
   if sorts    <>"" then sql=sql&" order by "&sorts

     Set rs=Server.CreateObject("ADODB.recordset")
	  if fenye then
         pagenum=cint(Request.QueryString("page"))
	   if pagenum="" or not isnumeric(pagenum) or pagenum<1 then pagenum=1

       rs.cachesize=page_size
       rs.cursortype=1
	 end if

	  if ty=1 then
	   rs.open sql,conn,1,1
	  elseif ty=2 then
	   rs.open sql,conn,1,2
	  elseif ty=3 then
	   rs.open sql,conn,1,3
	  end if

	 if fenye then
       rs.pagesize=page_size
        if pagenum>rs.pagecount then pagenum=rs.pagecount
        if not rs.eof then rs.absolutepage=pagenum
	 end if


  end sub

  public function ps(tf)
   if tf then
    ps=rs.pagesize
   elseif not tf then
     if rs.eof then
	  ps=true
	   else
	  ps=false
	 end if
   end if
  end function


  public function show(val)
    show=rs(val)
  end function

  public sub pagehtml(a,b,c)
   if pagenum=1 then
     Response.Write replace(replace(replace(a,"$1","javascript:"),"$2","dis"),"$3","首页")
     Response.Write replace(replace(replace(a,"$1","javascript:"),"$2","dis"),"$3","上一页")
	  else
     Response.Write replace(replace(replace(a,"$1","?page=1"),"$2","ok"),"$3","首页")
     Response.Write replace(replace(replace(a,"$1","?page="&pagenum-1&""),"$2","ok"),"$3","上一页")
   end if

   f=1
   g=10
   if g>rs.pagecount then g=rs.pagecount
   if pagenum>9 then
     f=pagenum-1
	 g=pagenum+9
   end if
   if g>rs.pagecount then
    f=rs.pagecount-10
	g=rs.pagecount
   end if

  for i=f to g
   if i=pagenum then
     Response.Write replace(b,"$1",i)
    else
     Response.Write replace(replace(replace(a,"$1","?page="&i&""),"$2","ok"),"$3",i)
	end if
  next

   if pagenum=rs.pagecount then
     Response.Write replace(replace(replace(a,"$1","javascript:"),"$2","dis"),"$3","下一页")
     Response.Write replace(replace(replace(a,"$1","javascript:"),"$2","dis"),"$3","尾页")
	  else
     Response.Write replace(replace(replace(a,"$1","?page="&pagenum+1&""),"$2","ok"),"$3","下一页")
     Response.Write replace(replace(replace(a,"$1","?page="&rs.pagecount&""),"$2","ok"),"$3","尾页")
   end if

    Response.Write replace(c,"$1","共"&rs.recordcount&"条记录")
	Response.Write replace(c,"$1","共"&rs.pagecount&"页")
	Response.Write replace(c,"$1","当前第"&pagenum&"页")
  end sub

  public function p(vas,y,f)
   if y=1 then
    mmm=Request.Form(vas)
   elseif y=2 then
    mmm=Request.QueryString(vas)
   end if
  if f=1 then
	 if mmm<>"" and mmm=1 and isnumeric(mmm) then
	   p=true
	  else
	   p=false
	 end if
	mmm=""
   else
    p=mmm
   end if
  end function

  public sub add()
    rs.addnew
  end sub

  public Sub into(jian,val)
    rs(jian)=val
   rs.update
  end Sub

  public sub del(tab,whe)
   sql="delete from "&tab
   if whe<>"" then sql=sql&" where "&whe
    conn.execute sql
  end sub

  public function x()
     x=rs.recordcount
  end function

  public sub nex()
   rs.movenext
  end sub

  public sub ul()
    rs.close
   Set rs=nothing
    sql=null
  end Sub

  public Sub cn()
    conn.close
   Set conn=nothing
  end Sub

  public function nullys(ys)
   nullys=true
   if ys="" then
     nullys=false
   end if
  end function

  public function rep(values,out)
    if Request.Form(values)="" then
      rep=out
	   else
	  rep=Request.Form(values)
	end if
  end function

end class
%>

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

Grow your business fast with

Suku