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.
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 ("暂时还没有文章")
else
Do Until rs.EOF
response.write("文章标题是:"& rs("cn_title"))
response.write("<br>文章作者是:"& rs("cn_author"))
response.write("<br>文章加入时间是:"& rs("cn_time"))
response.write("<br>文章内容是:"& 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 ("暂时还没有文章")
else
Do Until rs.EOF
response.write("文章标题是:"& rs("cn_title"))
response.write("<br>文章作者是:"& rs("cn_author"))
response.write("<br>文章加入时间是:"& rs("cn_time"))
response.write("<br>文章内容是:"& 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:
<%
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
%>
Now our showit.asp can write this:
showit.asp
<!--#include file="conn.asp" -->
<%
sql = "Select * from cnarticle"
opendatabase
rs.Open sql,conn,1,1
If not Rs.eof then
Do Until rs.EOF
response.write("文章标题是:"& rs("cn_title"))
response.write("<br>文章作者是:"& rs("cn_author"))
response.write("<br>文章加入时间是:"& rs("cn_time"))
response.write("<br>文章内容是:"& rs("cn_content"))
response.write(" rs.MoveNext
Loop
else
response.write ("暂时还没有文章")
end if
Closedatabase
%>
Well, we've written less, is this the easiest thing to do? Of course not! It can be simpler. Use the GetRows to pass the queried data to a variable and use the UBound method to get the data record bar number.
Don't understand? Okay, let's keep looking down:
Build another file: sql.asp
Sql.asp
<%
Class DataTable
public Function SelectData(sql)
If sql<>"" then
opendatabase
Rs.open sql,conn,1,1
If not Rs.eof then
Thedata=Rs.GetRows(-1)
Closedatabase
Else
Closedatabase
End If
End If
SelectData=Thedata
End Function
End Class
%>