This is my own experience, for you to refer to.
My goal is to make development simple, consider implementation statements as few as possible, and devote more energy to thinking about business logic. I hope my article will inspire and help you.
If you are not familiar with ASP, you can first read the following tutorial:
1. http://Bruce Wolf's ASP programming introduction
2. ASP tutorial (English/Chinese) of www.w3schools.com, see more
OK, let's get to the point:
Let’s take a look at the following examples:
<%
db_path="database/cnbruce.mdb"
Setconn=Server.CreateObject("ADODB.Connection")
connstr="Provider=Microsoft.Jet.OLEDB.4.0;DataSource="&Server.MapPath(db_path)
conn.Openconnstr
Setrs=Server.CreateObject("ADODB.Recordset")
sql="Select*fromcnarticle"
rs.Opensql,conn,1,1
ifrs.EOFandrs.BOFthen
response.write("No article yet")
else
DoUntilrs.EOF
response.write("Article title is:"&rs("cn_title"))
response.write("<br>The author of the article is: "&rs("cn_author"))
response.write("<br>The article is added to: "&rs("cn_time"))
response.write("<br>The content of the article is: "&rs("cn_content"))
response.write("<hr>")
rs.MoveNext
Loop
endif
rs.close
Setrs=Nothing
conn.close
setconn=Nothing
%>
Well, this is a typical example of reading data and displaying it, see: http://www.cnbruce.com/blog/showlog.asp?cat_id=26&log_id=448
Well, it's really simple. From top to bottom, it's easy to understand. But when you read, insert, delete and modify multiple tables, and when there are a lot of HTML/js in your code, you will have a question: Why are there so many things to repeat?
So generally we separate some simple operations and write them into classes or functions and put them into include files.
Then we can use two files to implement the above operation:
conn.asp
<%
db_path="database/cnbruce.mdb"
Setconn=Server.CreateObject("ADODB.Connection")
connstr="Provider=Microsoft.Jet.OLEDB.4.0;DataSource="&Server.MapPath(db_path)
conn.Openconnstr
%>
showit.asp
<!--#includefile="conn.asp"-->
<%
Setrs=Server.CreateObject("ADODB.Recordset")
sql="Select*fromcnarticle"
rs.Opensql,conn,1,1
ifrs.EOFandrs.BOFthen
response.write("No article yet")
else
DoUntilrs.EOF
response.write("Article title is:"&rs("cn_title"))
response.write("<br>The author of the article is: "&rs("cn_author"))
response.write("<br>The article is added to: "&rs("cn_time"))
response.write("<br>The content of the article is: "&rs("cn_content"))
response.write("<hr>")
rs.MoveNext
Loop
endif
rs.close
Setrs=Nothing