asp 常用代码

掌握这些代码,asp开发无忧。

1.连接数据库,每种语言都有自己连接数据库的方式,asp是通过ado来连接数据库的。

1.1以下代码是连接access的代码

<%
‘============定义变量=========================
dim conn,db,connstr
‘On Error Resume Next ‘容错声明
‘================填写数据库具体参数============
db=”liuyan.mdb” ‘数据库文件位置
mima=”ceshi”  ‘数据库密码,      如果数据库没有密码,这里的值随便写,一般为空值。注意数据库读写权限
‘===================数据库连接==================
Set conn = Server.CreateObject(“ADODB.Connection”)
connstr=”provider=microsoft.jet.oledb.4.0;data source=”&server.mappath(“”&db&”")&”;Jet Oledb:database password=”&mima&”"
if err then
err.clear
else
conn.open connstr
‘response.Write(“数据库连接成功!”)
end if
‘============结束程序=========================
%>

1.2 连接ms sql的代码:

 <%
Dim dbtype
dbType = “MSSQL”
Dim strConn
strConn = “driver={SQL Server};server=(local);uid=sa;pwd= ;database=数据库名”
Dim db
Set db = Server.CreateObject(“ADODB.Connection”)
db.Open strConn
%>

2.插入操作1

  H_title = request.Form(“H_title”)
H_sort = request.Form(“H_sort”)
H_font = request.Form(“H_font”)
H_Mood = request.Form(“H_Mood”)
H_Content = request.Form(“H_Content”)
H_uid = session(“uid”)
sql = “insert into H_Bless(UID,H_title,H_Content,H_sort,H_font,H_Mood)”
sql = sql&”values(“&H_uid&”,’”&H_title&”‘,’”&H_Content&”‘,”&H_sort&”,”&H_font&”,”&H_Mood&”)”
conn.Execute(sql)
conn.close
set conn = nothing

3.插入操作2

 dim rs
set rs=server.createobject(“adodb.recordset”)
sql=”select * from book”
rs.open sql,conn,1,3    ‘打开数据库,进行更改操作
rs.addnew     ‘rs数据库表添加新内容
rs(“name”)=request(“title”)
rs.update    ‘更新表
rs.close    ‘关闭

 

4.asp循环

<%
for i=2 to 6
%>
<font size=”<%=i%>”>
hello world!
</font> <br />
<%
next
%>

5.asp 查询

5.1查询多条记录

<%
set rs=server.CreateObject(“adodb.recordset”)
sql=”select * from book”
rs.open sql,conn,1,1
if not (rs.eof and rs.bof) then
do while not rs.eof
%>
<tr>
<td><%=rs(“name”)%></td>
<td><a href=”fledit.asp?id=<%=rs(” mce_href=”fledit.asp?id=<%=rs(“id”)%>”>修改</a></td>
<td><a href=”fldel.asp?id=<%=rs(” mce_href=”fldel.asp?id=<%=rs(“id”)%>”>删除</a></td>
</tr>
<%
rs.movenext
loop
end if
%>

5.2查询一条记录

<%
Dim rs
Set rs = Server.CreateObject(“ADODB.recordset”)
sql = “select  *  from xinwen where id=”&request(“id”)
rs.open sql,db,1,1
if rs.eof then
else
%>
<%=rs(“neirong”)%>
<%
end if
rs.close
set rs=nothing
%>

6.操作成功后跳转代码:

Response.Write(“<script type=”text/javascript”>
alert(‘添加成功!’);window.location=’insbook.asp’
</script>”)
response.End()

7.数据修改

<!–#include file=”conn.asp”–>    ‘调用外部链接数据库asp程序
<%
if request(“栏目名称”)=”值” then ‘判断是否修改
‘取得表单传递的数据
member_name=Trim(Request.Form(“member_name”))
……………..
remark=Trim(Request.Form(“remark”))
sql=”select * from member where m_id=”&m_id   ‘打开表
set rs=server.createobject(“adodb.recordset”)
rs.open sql,conn,1,3
rs(“member_name”)=member_name
……………..
rs(“remark”)=remark
rs.update
rs.close
set rs=nothing
end if
%>

8.数据删除

<!–#include file=”conn.asp”–>    ‘调用外部链接数据库asp程序
<%
if Trim(Request.form(“action”))=”del” then
ID = Trim(Request.form(“ID”))
set rs=server.createobject(“adodb.recordset”)
sql=”select * from pay where ID =”&ID
rs.open sql,conn,1,3
rs.delete
rs.close
end if
%>

asp 采集代码

<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>  
<%    
'功能:asp采集代码    
'作者:wangsdong    
'备注:支持原创程序,请保留此信息,谢谢    
url="http://sports.sina.com.cn/k/2008-09-15/04593948756.shtml"    
str=getHTTPPage(url)    
title=strcut(str,"<h1 id=""artibodyTitle"" style=""color:#03005C;"">","</h1>",2)    
content=strcut(str,"<!-- 正文内容 begin -->","<!-- 正文内容 end -->",2)    
response.write "新闻标题<br><b>"&title&"</b><br><br><br>新闻内容:<br>"&content    
Function getHTTPPage(url)    
On Error Resume Next    
dim http    
set http=Server.createobject("Microsoft.XMLHTTP")    
Http.open "GET",url,false    
Http.send()    
    if Http.readystate<>4 then    
    exit function    
    end if    
getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312")    
set http=nothing    
    If Err.number<>0 then    
    Response.Write "<p align=’center’><font color=’red’><b>服务器获取文件内容出错</b></font></p>"    
    Err.Clear    
    End If    
End Function    

Function BytesToBstr(body,Cset)    
dim objstream    
set objstream = Server.CreateObject("adodb.stream")    
objstream.Type = 1    
objstream.Mode =3    
objstream.Open    
objstream.Write body    
objstream.Position = 0    
objstream.Type = 2    
objstream.Charset = Cset    
BytesToBstr = objstream.ReadText    
objstream.Close    
set objstream = nothing    
End Function    

'截取字符串,1.包括起始和终止字符,2.不包括    
Function strCut(strContent,StartStr,EndStr,CutType)    
Dim strHtml,S1,S2    
strHtml = strContent    
On Error Resume Next    
    Select Case CutType    
    Case 1    
    S1 = InStr(strHtml,StartStr)    
    S2 = InStr(S1,strHtml,EndStr)+Len(EndStr)    
    Case 2    
    S1 = InStr(strHtml,StartStr)+Len(StartStr)    
    S2 = InStr(S1,strHtml,EndStr)    
    End Select    
    If Err Then    
    strCute = "<p align=’center’>没有找到需要的内容。</p>"    
    Err.Clear    
    Exit Function    
    Else    
    strCut = Mid(strHtml,S1,S2-S1)    
    End If    
End Function    
%>

 

asp 分页页面

<!--#include file="conn.asp"-->   
<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />  
<title>无标题文档</title>  
<mce:script type="text/javascript"><!--  
   function realDel(id){  
     if(confirm("真得要删除吗?")){  

        window.location = "delSingleAns.asp?id="+id;  
     }  
   }  
   function pagefun(page){  

   }  
// --></mce:script>  

<link href="images/form.css" mce_href="images/form.css" rel="stylesheet" type="text/css" />  
<link href="images/table.css" mce_href="images/table.css" rel="stylesheet" type="text/css" />  
</head>  

<body>  
<form action="editSingleAns.asp" id="thisform" method="post">  
  <input name="currentPage" type="hidden" value="" />  
  <fieldset>  
   <p><label for="name" accesskey="9">试题名称:</label>  
      <%  
        if request.form("sname")<>"" then   
         snameTemp = request.form("sname")   
        else   
         snameTemp = request.queryString("sname")   
        end if  
        sname = snameTemp  

      %>  
     <input name="sname" type="text" size="30" value="<%=sname%>" />  
     <label for="name" accesskey="9">课本名称:</label>   
   <select name="bookid" id="bookid">  
     <option selected value="-1">--请选择--</option>  
         <%  
set rs=server.CreateObject("adodb.recordset")     
sql1="select * from book"    
rs.open sql1,conn,1,1     
if not (rs.eof and rs.bof) then     
do while not rs.eof     
%>  

    <option value=<%=rs("id")%> <%if cint(request.querystring("bid"))=cint(rs("id")) or cint(request.form("bookid"))=cint(rs("id")) then response.write(" selected ") end if%>><%=rs("name")%></option>  
    <%     
rs.movenext     
loop     
end if    

%>  
   </select>  
     <input name="hiddenPage" type="hidden" value="" />  
    <input name="Input" type="submit" value="搜索" /></p>  
</fieldset>  

<%  

%>  

<table width="100%" id="mytable" >  
  <tr>  
    <th width="10%" scope="col">编号</th>  
    <th width="10%" scope="col">名称</th>   
    <th width="20%" scope="col">所属课程</th>  
    <th width="20%" scope="col">入库时间</th>  
     <th width="20%" scope="col">更新时间</th>  
    <th width="10%" scope="col">修改</th>  
    <th width="10%" scope="col">删除</th>  
  </tr>  
<%  
set rs=server.CreateObject("adodb.recordset")  
s = request.Form("s")  
'sname = request.Form("sname")  

if request.Form("bookid")<>"" then  
  bid = request.Form("bookid")  
else  
  bid = request.querystring("bid")  
end if  

sql = "select s.*,b.name from singleAns s,book b where s.bookId=b.id"   

if sname<>"" then  
  sql=sql&" and s.title like '%"&sname&"%'"  
end if  
if bid<>"" and bid<>"-1" then   
  sql=sql&" and b.id="&bid  
end if  
sql=sql&" order by s.updateTime desc"  

Set rs1=Server.CreateObject("ADODB.RecordSet")   
totalNumsql = "select count(*) as c from ("&sql&")"  

rs1.Open totalNumsql,conn,1,1  
totalCount = rs1("c")  '总数据  
pageSize = 5  
if request.querystring("page")="" then  
  currentPage = 1   '当前页  
else  
 currentPage = request.querystring("page")  
end if  
If totalCount mod pageSize =0 Then  
  totalPage = totalCount / pageSize  
  'totalPage = totalCount/pageSize  '总页数  
else  
   totalPage = int(totalCount / pageSize)+1   
end if  

'select * from (select top 9 * from (select s.*,b.name from singleAns s,book b where s.bookId=b.id order by s.updateTime desc)) where 'id not in (select top 6 id from (select s.*,b.name from singleAns s,book b where s.bookId=b.id order by s.updateTime desc))  

if pageSize*(currentPage-1) <> 0 then   
  sql = "select * from (select top "&pageSize*currentPage&" * from ("&sql&")) WHERE id NOT IN (select top "&pageSize*(currentPage-1)&" id from ("&sql&"))"  
end if  
if pageSize*(currentPage-1) = 0 then   
  sql = "select top "&pageSize*currentPage&" * from ("&sql&")"  
end if  

rs.open sql,conn,1,1  
if not (rs.eof and rs.bof) then  
do while not rs.eof  
%>  
  <tr>  
    <td><%=rs("id")%></td>  
    <td><input name="bookName" type="text" id="bookName" value="<%=rs("title")%>" /></td>  
    <td><%=rs("name")%></td>  
    <td><%=rs("insTime")%></td>  
    <td><%=rs("updateTime")%></td>  
    <td><a href="#" mce_href="#">修改</a></td>  

    <td><a href="#" mce_href="#" onclick="return realDel(<%=rs("id")%>);">删除</a></td>  
  </tr>  
<%  
rs.movenext  
loop  
end if  
%>    
</table>  

<table width="100%" border="1" align="center">  
  <tr>  
    <th scope="col">共<%=totalCount%>条数据 <%=pageSize%>条/页 当前<%=currentPage%>页 共<%=totalPage%>页  
    <%  
    tempfirst = "editSingleAns.asp?page=1"  
    if sname<>"" then  
      tempfirst = tempfirst&"&sname="&sname  
    end if  
    if bid<>"" then  
      tempfirst = tempfirst&"&bid="&bid  
    end if  
    %>  
    <a href="<%=tempfirst%" mce_href="&lt;%=tempfirst%">>首页</a>   

    <%  

    tempnext = "editSingleAns.asp?page="&currentPage-1    
    if sname<>"" then  
      tempnext = tempnext&"&sname="&sname  
    end if  
    if bid<>"" then  
      tempnext = tempnext&"&bid="&bid  
    end if  

       if currentPage = 1 then   
          response.write "上页"  
       else  
          response.write "<a href="&tempnext&" mce_href="&amp;tempnext&amp;">上页</a>"   
       end if  
       response.write "&nbsp;&nbsp;"  
    %>  

    <%  
    tempstr = "editSingleAns.asp?page="&currentPage+1    
    if sname<>"" then  
      tempstr = tempstr&"&sname="&sname  
    end if  
    if bid<>"" then  
      tempstr = tempstr&"&bid="&bid  
    end if  
    %>  

    <%  
       if int(currentPage)=int(totalPage) then   
         response.write "下页"      
       else  
         response.write "<a href=""&tempstr&"" mce_href="&quot;&amp;tempstr&amp;&quot;">下页</a>"   
       end if   

    %>  

    <%  
    templast = "editSingleAns.asp?page="&totalPage  
    if sname<>"" then  
      templast = templast&"&sname="&sname  
    end if  
    if bid<>"" then  
      templast= templast&"&bid="&bid  
    end if  
    %>  
    <a href="<%=templast%>">尾页</a>  
     跳转到<select name="page" id="page" onchange="window.location.href=this.value;">  
            <%  

              for i=1 to int(totalPage)  
                temp1 = "editSingleAns.asp?page="&i  
                if sname<>"" then  
                  temp1 = temp1&"&sname="&sname  
                end if  
                if bid<>"" then  
                  temp1 = temp1&"&bid="&bid  
                end if  

                response.write "<option value="&temp1&">"&i&"</option>"   
              next  
            %>  

     </select>  
     页     </th>  
  </tr>  
</table>  
</form>  
</body>  
</html>

 

ASP中数据的增、删、改、查

‘ms sql链接数据库配置语句

<%

Dim dbtype

dbType = “MSSQL”

Dim strConn

strConn = “driver={SQL Server};server=(local);uid=sa;pwd= ;database=数据库名”

Dim db

Set db = Server.CreateObject(“ADODB.Connection”)

db.Open strConn

%>

 
‘可单独存于目录下命名“conn.asp”;便于修改
……………………………………………………………………………………
数据显示

……………………………………………………………………………………

<!–#include file=”conn.asp”–>    ‘调用外部链接数据库asp程序

<%

dim rs

set rs=server.CreateObject(“adodb.Recordset”)

sql=”Select top 5 * from 表名 where 条件 order by ID desc”

rs.open sql,conn,1,1    ‘打开数据库,进行只读操作

if rs.eof then ‘判断数据库内有无信息,防止读取错误;bof:表示光标位置在第一条记录前 eof:表示光标位置在最后一条记录前

else

do while not rs.eof

%>
‘显示内容块,在要显示的地方插入
<li><a href=”链接页面.asp?id=<%=rs(“id”)%>”><%=rs(“biaoti”)%></a></li>

<%

‘循环显示数据

rs.movenext

loop

rs.close  ‘关闭数据库

set rs=nothing   ‘断开链接

 

‘在子页显示所有的“标题”

<%
Dim rs
Set rs = Server.CreateObject(“ADODB.recordset”)
sql = “select  *  from xinwen order by id desc”
rs.open sql,db,1,1
if rs.eof then
else
do while not rs.eof
%>
<li><a href=”02.asp?id=<%=rs(“id”)%>”><%=rs(“biaoti”)%></a></li>  ‘li 让内容按行显示
<%
rs.movenext
loop
end if
rs.close
set rs=nothing
%>
‘在文本页面显示”内容“

<%
Dim rs
Set rs = Server.CreateObject(“ADODB.recordset”)
sql = “select  *  from xinwen where id=”&request(“id”)
rs.open sql,db,1,1
if rs.eof then
else    %>
<%=rs(“neirong”)%>
<%
end if
rs.close
set rs=nothing
%>

 
数据写入

……………………………………………………………………………………

<!–#include file=”conn.asp”–>    ‘调用外部链接数据库asp程序

<%

dim rs

set rs=server.createobject(“adodb.recordset”)

sql=”select * from 表名”

rs.open sql,conn,1,3    ‘打开数据库,进行更改操作

rs.addnew     ‘rs数据库表添加新内容

rs(“name”)=request(“name”)

rs(“Tel”)=request(“tel”)

rs(“fax”)=request(“fax”)

rs(“mail”)=request(“email”)

rs(“code”)=request(“zip”)

rs.update    ‘更新表

rs.close    ‘关闭

%>
数据修改

……………………………………………………………………………………

<!–#include file=”conn.asp”–>    ‘调用外部链接数据库asp程序

<%

if request(“栏目名称”)=”值” then ‘判断是否修改

‘取得表单传递的数据

member_name=Trim(Request.Form(“member_name”))

……………..

remark=Trim(Request.Form(“remark”))

sql=”select * from member where m_id=”&m_id   ‘打开表

set rs=server.createobject(“adodb.recordset”)

rs.open sql,conn,1,3

rs(“member_name”)=member_name

……………..

rs(“remark”)=remark

rs.update

rs.close

set rs=nothing

end if

%>

 

数据删除

……………………………………………………………………………………

<!–#include file=”conn.asp”–>    ‘调用外部链接数据库asp程序

<%

if Trim(Request.form(“action”))=”del” then

ID = Trim(Request.form(“ID”))

set rs=server.createobject(“adodb.recordset”)

sql=”select * from pay where ID =”&ID

rs.open sql,conn,1,3

rs.delete

rs.close

end if

%>

asp常用代码

定义数据库连接

<%  
'============定义变量=========================   
dim conn,db,connstr  
'On Error Resume Next '容错声明   
'================填写数据库具体参数============  
db="liuyan.mdb" '数据库文件位置  
mima="ceshi"  '数据库密码,      如果数据库没有密码,这里的值随便写,一般为空值。注意数据库读写权限  
'===================数据库连接==================   
Set conn = Server.CreateObject("ADODB.Connection")  
connstr="provider=microsoft.jet.oledb.4.0;data source="&server.mappath(""&db&"")&";Jet Oledb:database password="&mima&""  
if err then  
    err.clear  
else  
    conn.open connstr  
'response.Write("数据库连接成功!")  
end if  
'============结束程序=========================   
%>

插入操作1

H_title = request.Form("H_title")  
  H_sort = request.Form("H_sort")  
  H_font = request.Form("H_font")  
  H_Mood = request.Form("H_Mood")  
  H_Content = request.Form("H_Content")  
  H_uid = session("uid")  
  sql = "insert into H_Bless(UID,H_title,H_Content,H_sort,H_font,H_Mood)"  
  sql = sql&"values("&H_uid&",'"&H_title&"','"&H_Content&"',"&H_sort&","&H_font&","&H_Mood&")"  
  conn.Execute(sql)  
  conn.close  
  set conn = nothing  

  response.Write("<mce:script type="text/javascript"><!--  
alert('添加成功')  
// --></script>")  
  response.Write("<mce:script type="text/javascript"><!--  
window.location.href='../Bless.asp'  
// --></script>")

插入操作2

dim rs  

set rs=server.createobject("adodb.recordset")  

sql="select * from book"  

rs.open sql,conn,1,3    '打开数据库,进行更改操作  

rs.addnew     'rs数据库表添加新内容  

rs("name")=request("title")  

rs.update    '更新表   

rs.close    '关闭

asp循环

<%   
for i=2 to 6   
%>   
<font size="<%=i%>">   
hello world!   
</font> <br />  
<%   
next   
%>

asp 查询:

<%  
set rs=server.CreateObject("adodb.recordset")  
sql="select * from book"  
rs.open sql,conn,1,1  
if not (rs.eof and rs.bof) then  
do while not rs.eof  
%>  
  <tr>  
    <td><%=rs("name")%></td>  
    <td><a href="fledit.asp?id=<%=rs(" mce_href="fledit.asp?id=<%=rs("id")%>">修改</a></td>  
    <td><a href="fldel.asp?id=<%=rs(" mce_href="fldel.asp?id=<%=rs("id")%>">删除</a></td>  
  </tr>  
<%  
rs.movenext  
loop  
end if  
%>

操作成功后跳转代码:

Response.Write("<script type="text/javascript">  
alert('添加成功!');window.location='insbook.asp'  
</script>")  
response.End()

 

asp连接access实例

一直想建一个自己的小网站,可是jsp空间昂贵,还是asp的吧。找啦许多cms,学习成本太高,于是非下决心学习一下。先看一个连接数据库的,能连数据库了,其它的都OK了。
首先,建一目录如:D:/asp/test,三个文件db.mdb,conn.asp,showdata.asp。
conn.asp:

<%
Set conn = Server.CreateObject("ADODB.Connection")
conn.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ="&Server.MapPath("db.mdb")
%>

showdata.asp:

<!--#include file="conn.asp"--> 
<%
set rs=server.CreateObject("adodb.recordset")
sqlstr="select * from user"
rs.open sqlstr,conn,1,3 
rs.movefirst 
while not rs.eof
response.write(rs("name"))
rs.movenext 
wend
rs.close
conn.close 
set rs=nothing
set conn=nothing
%>

asp的脚本很奇怪,rs.open sqlstr,conn,1,3 参数之间用”,”隔开,相当于rs.open(sqlstr,conn,1,3)

最后,在把IIS的主目录指向D:/asp/test
浏览 http://localhost/showdata.asp