将数据库里的数据以html静态页面的方式呈现,不用说,大家都知道好处非常多。网站的数据量不断增大,动态的新闻页面每次都需要读取数据库信息,给服务器数据库带来很大压力,读取速度越来越缓慢,负荷过大,宕机也在所难免。通过asp程序把数据库里的信息转化为html静态页面,不仅可以大大加速网页的浏览速度,而且减轻了服务器压力,而且静态的html对于搜索引擎来说,也很友好,可以吸引更多的“蜘蛛”来抓取信息,提高网站访问量。

  这2天考虑到网站的数据优化问题,决定把数据库里的动态数据全部生成html页面。原本希望能直接从网上下载这方面的程序,找了半天也没有找到适合自己的。最后只找到一个添加文章后直接生成html页面的程序。而我需要的是直接把数据库里的数据按照固定模板通过asp直接生成。没办法,既然没有就在此基础上改吧。

一、建立数据库database.mdb
  首先,建立一个表,命名为c_news,表中两字段分别为id(编号),,c_title (标题),,c_name (名称),,c_content (正文),,c_filepath (生成的静态页面的路径),,c_time (时间)。

二、建立数据连接文件conn.asp,代码如下:

  以下是引用片段:
<%
set conn = Server.CreateObject("ADODB.Connection")
connstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="&Server.MapPath("database.mdb")
conn.Open connstr
%>

三、建立一个模板页面,代码如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title> {$c_title$} </title>
</head>

<body>
<table width="80%" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td height="80"><div align="center"><strong> {$c_title$} </strong></div></td>
  </tr>
  <tr>
    <td height="50"><div align="center"> {$c_name$} </div></td>
  </tr>
  <tr>
    <td height="200"> {$c_content$} </td>
  </tr>
  <tr>
    <td height="50"> {$c_time$} </td>
  </tr>
</table>
</body>
</html>

四、建立以下主页面,代码如下:

<!--#include file="conn.asp"-->
<HTML><HEAD><TITLE>生成页面</TITLE>
<META http-equiv=Content-Type content="text/html; charset=gb2312">
</HEAD>
<BODY bottomMargin=0 leftMargin=0 topMargin=0 rightMargin=0>
<%
dim id
id=cint(request.querystring("id"))
if id="" then
id="1"
else
id=cint(request.querystring("id"))
end if

Dim totalnumber
sql="select id from c_news order by id DESC"
set rs=server.createobject("adodb.recordset")
rs.open sql,conn,1,1
rs.Movefirst
if not rs.eof then
TotalNumber=rs.recordcount
end if
rs.close
set rs=nothing

Response.Write "<br><br>" & vbNewLine
Response.Write "<table width='400' border='0' align='center' cellpadding='0' cellspacing='0'>" & vbNewLine
Response.Write " <tr>" & vbNewLine
Response.Write "    <td height='50'>总共 <font color='blue'><b>" & totalnumber & "</b></font> 个页面,正在生成第 <font color='red'><b>" & ID & "</b></font> 个页面…… </td>" & vbNewLine
Response.Write " </tr>" & vbNewLine
Response.Write "</table>" & vbNewLine
Response.Flush

If id > totalnumber Then
Response.Write "恭喜您操作成功!"
Response.Flush
End If

creat(ID)
id=id+1
Response.Write "<meta http-equiv=""refresh"" content=""1;url=?ID="&id&""">"
%>
</BODY>    
</HTML>

<%
Function creat(id)
sql="SELECT * from c_news where id="&id
set rs=conn.execute(sql)
if rs.bof and rs.eof then
 name="记录集合为空"
else
dim name,title,content,time
id=rs("id")
name=rs("c_name")
title=rs("c_title")
content=rs("c_content")
time=rs("c_time")end if
conn.execute(sql)
rs.close
set rs=nothing

dim fso
dim znwl
dim art
dim fw
dim fname,folder,filepath

'生成HTML文件名,建立文件夹,指定文件路径 
fname = id
folder = "html/"
format = ".html"
filepath = folder&fname&format
set fso=createobject("scripting.filesystemobject")
set znwl=fso.opentextfile(server.mappath("moban.html"))
art=znwl.readall
znwl.close

art=replace(art,"{$c_name$}",name)
art=replace(art,"{$c_title$}",title)
art=replace(art,"{$c_content$}",content)
art=replace(art,"{$c_time$}",time)

'生成HTML页面
set fw=fso.createtextfile(server.mappath(filepath),true)
fw.writeline art
fw.close
set fso=nothing
End Function
%>

这个asp通过模板生成html静态页面的程序并不太复杂,小程序看似简单,实用性却非常大,通过简单修改,就能巧妙的运用到适合你自己的网站中。 我把这个小程序打了包,有需要也可以直接在这里下载,很小,只有10K。