⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 op.asp

📁 ASP + AJAX留言板 学习AJAX好代码
💻 ASP
字号:
<%
'错误代码
'1:action参数错误
'2:读取数据失败
'3:添加/修改数据时数据不完整
'4:添加数据失败
'5:要删除的数据的id不正确
'6:要删除的数据的id不存在
'7:删除数据失败
'8:修改的id不正确
'9:修改的id不存在
'10:修改失败
option explicit
dim cn,rs,connstr,action,sql
dim currentpage,pagecount,totalpage,totalrecord
currentpage=0
pagecount=0
totalpage=0
totalrecord=0
dim xml:xml="<?xml version='1.0' encoding='gb2312'?><body>"
action=request.QueryString("action")&""

select case action
  case "read"
    call ReadData
  case "del"
    call Del
  case "up"
    call Update
  case "add"
    call Add
  case else
    xml=xml&"<errcode>1</errcode>"
end select

xml=xml&"</body>"
response.Clear
response.ContentType="text/xml"
response.CharSet="gb2312"
response.Write xml
response.End

sub Del
  dim fid,minid,affectrecord
  fid=request.QueryString("id")&""
  minid=request.QueryString("minid")&""
  if not isnumeric(fid) then
    xml=xml&"<errcode>5</errcode>"
    exit sub
  end if
  on error resume next
  sql="delete from data where fid="&fid
  call openDB 
  cn.Execute sql,affectrecord,1+128
  sql=""
  if isnumeric(minid) then sql="select top 1 * from data where fid<"&minid&" order by fid desc"
  if affectrecord=0 then '要删除的id不存在
    xml=xml&"<errcode>6</errcode>"
  elseif sql<>"" then
    set rs=cn.Execute(sql)
    if not rs.EOF then'如果有最后一条记录,返回这条记录的值
      xml=xml&"<item>"
      xml=xml&"<id>"&rs(0)&"</id>"
      xml=xml&"<ti><![CDATA["&rs(1)&"]]></ti>"
      xml=xml&"<ct><![CDATA["&replace(rs(2),"<br>","\n")&"]]></ct>"
      xml=xml&"</item>"
    end if
    rs.Close
    set rs=nothing
  end if
  call closeDB  
  if err<>0 then
   xml=xml&"<errcode>7</errcode>"
   xml=xml&"<errmsg><![CDATA["&err.Description&"|"&err.Source&"]]></errmsg>"
  end if
end sub

sub Update
  dim fid,ti,ct
  fid=request.Form("id")&""
  ti=request.Form("ti")&""
  ct=replace(request.Form("ct")&"",chr(10),"<br>")
  if not isnumeric(fid) then
    xml=xml&"<errcode>8</errcode>"
    exit sub
  end if
  if ti="" or ct="" then
    xml=xml&"<errcode>3</errcode>"
    exit sub
  end if
  on error resume next
  sql="select * from data where fid="&fid
  set rs=server.CreateObject("adodb.recordset")
  call openDB 
  rs.open sql,cn,1,3,1
  if rs.bof and rs.eof then
    xml=xml&"<errcode>9</errcode>"
  else
   rs("title")=ti
   rs("content")=ct
   rs.update
  end if
  rs.close
  set rs=nothing
  call closeDB
  if err<>0 then
    xml=xml&"<errcode>10</errcode>"
    xml=xml&"<errmsg><![CDATA["&err.Description&"|"&err.Source&"]]></errmsg>"
  end if
end sub

sub Add
  dim t,ct
  t=request("t")'.Form("t")&""
  ct=request("ct")'replace(request.Form("ct")&"",chr(10),"<br>")
  if t="" or  ct="" then
    xml=xml&"<errcode>3</errcode>"
    exit sub
  end if
  sql="data"
  set rs=server.CreateObject("adodb.recordset")
  on error resume next
  call openDB
  rs.open sql,cn,1,3,2
  rs.AddNew
  rs("title")=t
  rs("content")=ct
  rs.Update
  rs.MoveLast'移动到最后一条记录,即当前添加的记录,返回这条记录的id给客户端
  xml=xml&"<id>"&rs(0)&"</id>"
  rs.Close
  set rs=nothing
  call closeDB 
  if err<>0 then 
    xml=xml&"<errcode>4</errcode>"
    xml=xml&"<errmsg><![CDATA["&err.Description&"]]></errmsg>"
  end if
end sub


sub ReadData
  call openDB  
  call getTBInfo  
  dim init:init=request.querystring("init")&""
  if init="1" then
    if totalrecord=0 then
      xml=xml&"<tbinfo><tr>0</tr></tbinfo>"
      call closeDB
      exit sub
     else
      xml=xml&"<tbinfo><tr>"&totalrecord&"</tr></tbinfo>"
     end if
  end if
  currentpage=request.QueryString("p")&""    
  if currentpage<>"" and isnumeric(currentpage) then
    currentpage=cint(currentpage)
  else
    currentpage=1
  end if
  if currentpage<1 or currentpage>totalpage then currentpage=1
  '使用sql进行分页
  if currentpage=1 then 
    sql="select top "&pagecount&" * from data order by fid desc"
  else
    sql="select top "&pagecount&" * from data where fid< ( "&_
        "select min(fid) from "&_
        "(select top "&((currentpage-1)*pagecount)&" fid from data order by fid desc))"&_
        " order by fid desc"
  end if
  on error resume next
  set rs=cn.Execute(sql)
  xml=xml&"<list>"
  while not rs.EOF
    xml=xml&"<item>"
    xml=xml&"<id>"&rs(0)&"</id>"
    xml=xml&"<ti><![CDATA["&rs(1)&"]]></ti>"
    xml=xml&"<ct><![CDATA["&replace(rs(2),"<br>","\n")&"]]></ct>"
    xml=xml&"</item>"
    rs.movenext
  wend
  xml=xml&"</list>"
  rs.close
  set rs=nothing 
  call closeDB
  if err<>0 then xml=xml&"<errcode>2</errcode>"
end sub

sub getTBInfo
  sql="select count(*) from data"
  set rs=cn.Execute(sql)
  totalrecord=cint(rs(0))
  rs.Close  
  set rs=nothing

  pagecount=request.QueryString("pc")&""
  if isnumeric(pagecount) then
    pagecount=cint(pagecount)
    if pagecount<0 then pagecount=10
  else
    pagecount=10
  end if

  if totalrecord mod pagecount =0 then
    totalpage=totalrecord \ pagecount
  else
    totalpage=totalrecord \ pagecount +1
  end if
end sub

sub openDB
 connstr="provider=microsoft.jet.oledb.4.0;data source="&server.MapPath("data.mdb")
 set cn=server.CreateObject("adodb.connection")
 cn.open connstr
end sub

sub closeDB
 cn.Close
 set cn=nothing
end sub
%>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -