Common ASP code

Source: Internet
Author: User
(Genuine brand new)-ASP technical solution (with a CD)
$60.0
 

With this knowledge of the Code, ASP development is free of worries.

1. Connect to the database. each language has its own connection to the database. asp connects to the database through ADO.

1.1 The following code is the access connection code.

<%
'============== Definition variable ======================================
Dim Conn, DB, connstr
'On error resume next' Fault Tolerance statement
'======================== Fill in the specific database parameters ================
DB = "liuyan. mdb" 'database File Location
Mima = "Ceshi" 'database password. If the database does not have a password, the value here is left empty. Pay attention to database Read and Write Permissions
'=============================== Database connection ==========================
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 ("database connection successful! ")
End if
'============== End program ======================
%>

1.2 code for connecting ms SQL:

<%
Dim dbtype
Dbtype = "MSSQL"
Dim strconn
Strconn = "driver = {SQL Server}; server = (local); uid = sa; Pwd =; database = database name"
Dim DB
Set DB = server. Createobject ("ADODB. Connection ")
DB. Open strconn
%>

2. insert operation 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. insert operation 2

Dim rs
Set rs = server. Createobject ("ADODB. recordset ")
SQL = "select * from book"
Rs. Open SQL, Conn, 'Open the database and perform the change operation
Add new content to the RS. addnew'rs database table
RS ("name") = request ("title ")
Rs. Update' update table
Rs. Close 'Close



 

4. asp Loop

<%
For I = 2 to 6
%>
<Font size = "<% = I %>">
Hello world!
</Font> <br/>
<%
Next
%>

5. asp Query

5.1 query multiple records

<%
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") %> "> modify </a> </TD>
<TD> <a href = "FlDEL. asp? Id = <% = RS ("mce_href =" FlDEL. asp? Id = <% = RS ("ID") %> "> Delete </a> </TD>
</Tr>
<%
Rs. movenext
Loop
End if
%>

5.2 query a record


<%
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. Jump to the code after the operation is successful:

Response. Write ("<SCRIPT type =" text/JavaScript ">
Alert ('added successfully! '); Window. Location = 'insbook. asp'
</SCRIPT> ")
Response. End ()

7. data modification

<! -- # Include file = "conn. asp" --> 'Call the external link database ASP program
<%
If request ("topic name") = "value" then' determine whether to modify
'Get the data passed by the form
Member_name = trim (request. Form ("member_name "))
.................
Remark = trim (request. Form ("remark "))
SQL = "select * from member where m_id =" & m_id 'Open the table
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. data deletion

<! -- # Include file = "conn. asp" --> 'Call the external link database ASP program
<%
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
%>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.