使用ASP方便的建立自己网站的每日更新
发布时间:2006-10-14 2:39:27   收集提供:gaoqian
使用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放到存储过程中去

 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50