使用asp方便的建立自己网站的每日更新
每日更新是什么东东我想大家也都应该知道把,
其实有点象现在很多新闻网站的更新,下面介绍如何让你的
网站的内容每天自动更新
下面的代码适用于:
1.使用任何odbc兼容的数据库
2。很方便的插入到你现有的asp程序中
如何保存更新内容呢?
数据库结构:(一共三个字段)
quoteid(long ),quote(string ),author(string)
下面一个技巧是如何让更新显示在任意一个页面上呢?
我们只要把更新内容和作者当返回值送给调用的页面即可。
代码如下,其中logic是一个随机数,表示随机从数据库中显示哪个记录:
<%
sub getquote(byval strquote, byval strauthor)
dim intmaxid
dim intrecordid
dim strsql
dim oconn
dim ors
set oconn = server.createobject("adodb.connection")
oconn.open "database=mydb;dsn=quotes;uid=sa;password=;"
strsql = "select maxid=max(quoteid) from quotes"
set ors = oconn.execute(strsql)
if ors.eof then
strquote = "站长太懒了,今天没有更新内容."
strauthor = "呵呵"
exit sub
else
intmaxid = ors("maxid")
end if
randomize
intrecordid= int(rnd * intmaxid) + 1
strsql = "select * from quotes where quoteid=" & intrecordid & ";"
set ors = oconn.execute(strsql)
if ors.eof then
strquote = "站长太懒了,今天没有更新内容."
strauthor = "呵呵"
exit sub
else
ors.movefirst
strquote = ors("quote")
strauthor = ors("author")
end if
ors.close
oconn.close
set ors = nothing
set oconn = nothing
end sub
%>
其实在程序中如果使用一个嵌套的sql能够提高性能,例如这样
select * from quotes where quoteid = (select int ( rnd * max(quoteid) ) from quotes );
可是问题是有些数据库的随机数函数是rand而不是rnd,
如果要是你自己用的话,那当然可以使用这句话代替我上面介绍的方法,
可别忘了,要是别人的数据库不支持rand怎么办,呵呵。
再说了,现在是在讲asp技术,而不是在讲sql技术,呵呵。
现在我们将上面的代码保存到一个名叫quotes.inc的文件中来,
下面就举一个如何调用它的例子把:
<html>
<head>
<title>例子</title>
<!–#include virtual = "quotes.inc" –>
</head>
<body>
<br><br>
<%
dim strquote
dim strauthor
getquote(strquote, strauthor)
%>
<table border=0 cellpadding=6 cellspacing=5 bgcolor="#000000" width=500>
<tr bgcolor="#cccccc">
<td align=center>
<b>"<% =strquote %>" <br>–<i><% =strauthor %></i></b>
</td>
</tr>
</table>
<br><br>
</body>
</html>
其实你可以再加强点它的功能:
1.可以在子过程中给返回的字符串带上格式,这样显示会更加漂亮
2。将这个代码做成一个组件来调用
3。使用一个文本文件来代替数据库
4。将sql放到存储过程中去
