Recommend a good novice ASP programming Basic Law 1th/2 page _ Application Tips

Source: Internet
Author: User
Tags access database
First, the rookie often make mistakes
In the forum to see a lot of posts in the Code have a common basic error, field type error.
The program and the database are tightly connected, and the database field text or time type uses single quotes
For example, the following amendment statement:
Conn.execute "Update Counts set counts= '" &counts& "where num=" &num& "and Atime=" "&now () &" "
The Equals sign is the field name on the left. The right side of the equals sign is the variable name, the Counts field is literal, so the write must be preceded by a single quotation mark, whether written or queried, and in the following search statement, the NUM field is numeric, so there is no single quotation mark before and after, Atime The field is a time type, so you also need to add a single quotation mark around it.
The most important is the ID query, the ID field is unique and the number type, it is obvious that the query ID number is not before and after the single quotation mark

Conn.execute "Update Counts set counts= '" "&counts&" ' Where id= ' "&id&" ' "' wrong notation
Conn.execute "Update Counts set counts= '" "&counts&" ' where id= ' &id ' correct wording

Second, ACCESS database connection
There are usually two ways to connect to a database, and the novice has little idea of which way to use, or under what circumstances, or what the two principles are.
① direct connection to a database file


Set conn = Server.CreateObject ("ADODB. Connection ")
Conn. Open "Driver={microsoft Access DRIVER (*.mdb)}; Dbq= "&server.mappath (" Database/yanhang.mdb ")

② connection to database files through a data source


Set conn = Server.CreateObject ("ADODB. Connection ")
Conn. Open "Provider=Microsoft.Jet.OLEDB.4.0; Data source= "&server.mappath (" Database/yanhang.mdb ")

So which of the two is good? Of course, the second, because the first is actually the client browser directly read the database, so the security aspect is much different, the second through the data source connection, is the Server data Source Tool connection, and the client does not matter, so the database will not be exposed to the client, a high safety factor.

Application of ACCESS Database counterpart: ① direct connection to database files


Conn. Open "Driver={microsoft Access DRIVER (*.mdb)}; Dbq= "&server.mappath (" Database/yanhang.mdb ")

This way of database connection, add statements:


Set Rs=server.createobject ("Adodb.recordset") ' (correct notation)
Rs.Open "SELECT * from Dndj", conn,1,3
Rs.addnew
RS ("bh") = BH
RS ("bm") = BM
RS ("xm") = XM
RS ("xsq") = xsq
Rs.update
Rs.close
Set rs=nothing

Set Rs=server.createobject ("Adodb.recordset") ' (wrong notation)
Sql= "INSERT into DNDJ (BH,BM,XM,XSQ) VALUES (' bh ', ' BM ', ' xm ', ' xsq ')"
Rs.Open sql,conn,1,3

Application of ACCESS Database counterpart: ② Connect database files through a data source


Conn. Open "Provider=Microsoft.Jet.OLEDB.4.0; Data source= "&server.mappath (" Database/yanhang.mdb ")

This way of database connection, add statements:


Conn.execute "INSERT into DNDJ (BH,BM,XM,XSQ) VALUES (' &bh&" ', ' "&bm&" ', ' "&xm&" ', ' "") &xsq & "')" (correct writing)

Set Rs=server.createobject ("Adodb.recordset") ' (wrong notation)
Sql= "INSERT into DNDJ (BH,BM,XM,XSQ) VALUES (' bh ', ' BM ', ' xm ', ' xsq ')"
Rs.Open sql,conn,1,3

Three, double quotation mark's application
Usually we write super connections so <a href= "abc.asp?id=<%=rs (" id ")%>" > Super Connect </a>
But if you compile this super connection into the ASP,


Response.Write "<a href=" "Abc.asp?id=" &rs ("id") & "" > Super Connection </a> "" (correct)
Response.Write "<a href= ' abc.asp?id=" &rs ("id") & "' > Super Connect </a>" (correct writing)
Response.Write "<a href=abc.asp?id=" &rs ("id") & "> Super Connection </a>" (correct writing)

Response.Write "<a href=" abc.asp?id=<%=rs ("id")%> "> Super Connect </a>" (incorrect notation)
Response.Write "<a href=" abc.asp?id= &rs ("id") & "> Super Connection </a>" (Wrong writing)

Form compiled into ASP <input type= "text" name= "id" value= "<%rs (" id ")%>"/> "


Response.Write "<input type=" "Text" name= "id" "Value=" "&rs (" id ") &" "/>" (correct) Note: Here are three double quotes
Response.Write "<input type= ' text ' name= ' id ' value= '" &rs ("id") & "'/>" (correct writing)
Response.Write "<input type=text name=id value=" &rs ("id") & "/>" (correct notation)

Response.Write "<input type=" text "name=" id "value=" <%=rs ("id")%> "/>" (incorrect notation)
Response.Write "<input type=" text "name=" id "value=" "&rs" ("id") & "/>" (Error writing)
Current 1/2 page 12 Next read the full text
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.