ASP rapid development method of data Operation instance code 1th/3 Page _asp Foundation

Source: Internet
Author: User
Tags add time
This is my own experience, for you to make a reference.
My goal is to make development simple, consider the implementation statements as little as possible, and devote more effort to thinking about business logic. I hope my article will inspire and help you.
If you are unfamiliar with ASP, you can first look at the following tutorial:
1, http://Bruce Wolf's ASP Programming entry advanced
2. Www.w3schools.com's ASP tutorial (English/Chinese), see more

Okay, let's get to the point:
Let's look at the following examples:
<%
Db_path = "Database/cnbruce.mdb"
Set conn= Server.CreateObject ("ADODB.") Connection ")
ConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" &server.mappath (Db_path)
Conn. Open ConnStr
Set rs = Server.CreateObject ("ADODB.") Recordset ")
sql = "SELECT * FROM Cnarticle"
Rs. Open sql,conn,1,1
If Rs. EOF and Rs. BOF Then
Response.Write ("no article Yet")
Else
Do Until Rs. Eof
Response.Write ("article title is:" & RS ("Cn_title"))
Response.Write ("<br> article author is:" & RS ("Cn_author"))
Response.Write ("<br> article Add Time is:" & RS ("Cn_time"))
Response.Write ("<br> article content is:" & RS ("Cn_content"))
Response.Write ("Rs. MoveNext
Loop
End If
Rs.close
Set rs = Nothing
Conn.close
Set conn=nothing
%>
Well, this is a typical example of reading data and showing 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 and Html\js multiple tables, when you have a lot of mixed in your code, you have a question: why are there so many things to repeat?
So we usually separate some simple operations, write classes or functions into include files (include).

So we can use the above two files to achieve the following:
Conn.asp
<%
Db_path = "Database/cnbruce.mdb"
Set conn= Server.CreateObject ("ADODB.") Connection ")
ConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" &server.mappath (Db_path)
Conn. Open ConnStr
%>
Showit.asp
<!--#include file= "conn.asp"-->
<%
Set rs = Server.CreateObject ("ADODB.") Recordset ")
sql = "SELECT * FROM Cnarticle"
Rs. Open sql,conn,1,1
If Rs. EOF and Rs. BOF Then
Response.Write ("no article Yet")
Else
Do Until Rs. Eof
Response.Write ("article title is:" & RS ("Cn_title"))
Response.Write ("<br> article author is:" & RS ("Cn_author"))
Response.Write ("<br> article Add Time is:" & RS ("Cn_time"))
Response.Write ("<br> article content is:" & RS ("Cn_content"))
Response.Write ("Rs. MoveNext
Loop
End If
Rs.close
Set rs = Nothing
Conn.close
Set conn=nothing
%>
Reference: http://www.cnbruce.com/blog/showlog.asp?cat_id=26&log_id=448

Now relatively simple, if there are more than one operation page We just import the connection file can, but still not concise enough, where is not concise?
You've been creating the server, writing close all the time, so it's easy to go wrong, and it doesn't seem to have much to do with content.

Then I will improve the following:
Change the conn.asp file into:
Copy Code code as follows:

<%
Dim Conn
Dim Rs
Sub CloseDatabase
Conn.close
Set Conn = Nothing
End Sub
Sub OpenDatabase
Dim Strserver,struid,strsapwd,strdbname
strserver= "192.168.1.1" Database server name
struid= "sa" ' Your login account
Strsapwd= "" ' Your login password
Strdbname= "Cnbruce.mdb" ' Your database name
Set Conn = Server.CreateObject ("ADODB. Connection ")
' To connect to access
conn.connectionstring = "Provider=Microsoft.Jet.OLEDB.4.0; Data source= "& Server.MapPath (strDbName)
' Used to connect MSSQL
' conn.connectionstring = ' driver={sql server};d river={sql server};server= ' &StrServer&; uid= ' &struid & ";p wd=" &StrSaPwd& ";d atabase=" &strdbname
Set Rs=server. CreateObject ("ADODB.") RecordSet ")
Conn.Open
If ERR Then
Err. Clear
Set Conn = Nothing
Gbl_chk_tempstr = gbl_chk_tempstr & "Database connection Error!"
Response.Write Gbl_chk_tempstr
Response.End
End If
End Sub
%>

Current 1/3 page 123 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.