asp 常用代碼

來源:互聯網
上載者:User
(正版全新)-ASP技術方案寶典(附光碟片)
60.0元
 

掌握這些代碼,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
%>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.