星期二, 5月 15, 2007

讀取 SQL VBS 程式範例

上禮拜有人詢問 RSS VBS 的範例

範例如下:

' DB Informat
Const DB_Server = "localhost"
Const DB_User = "user"
Const DB_Passwd = "passwd"
Const DB_Database = "database"

dsn = "Driver={SQL Server};server=" & DB_Server & ";uid=" & DB_User & ";pwd=" & DB_Passwd & ";database=" & DB_Database
set conn = CreateObject("ADODB.Connection")

conn.Open dsn

set rs = CreateObject("ADODB.recordset")
sql = "select * from Message where (edate +1 >= { fn NOW()}) order by edate "
rs.open sql,conn,1,1

Set fs = CreateObject("Scripting.FileSystemObject")
File = "d:\rss_msg_" & filename & ".xml"
set txt = fs.CreateTextFile(File,True)

txt.writeline "<?xml version='1.0' encoding='Big5'?>"
txt.writeline "<rss version='2.0'>"
txt.writeline "<channel>"
txt.writeline "<title>xxxx</title>"
txt.writeline "<link>http://www.com.tw/xxxx</link>"
txt.writeline "<description>abc</description>"

while rs.eof <> true
mcontent_str = replace(rs("a1"),"&","")
mcontent_str = replace(mcontent_str,"<!--","")
mcontent_str = replace(mcontent_str,"-->","")
mcontent_str = replace(mcontent_str,Chr(10)," ")
mcontent_str = replace(mcontent_str,"<","&>")
mcontent_str = replace(mcontent_str,">","&<")

mtitle_str = replace(rs("a2"),"&","")

txt.writeline "<item>"
txt.writeline "<title>" & rs("a3") & "</title>"
txt.writeline "<link>" & rs("a4") & "</link>"
txt.writeline "<description>" & rs("a5") & "</description>"
txt.writeline "<country>" & rs("a6") & "</country>"
txt.writeline "<type>" & rs("a7") & "</type>"
txt.writeline "<pubDate>" & rs("a8") & "</pubDate>"
txt.writeline "</item>"

rs.movenext
wend

txt.writeline "</channel>"
txt.writeline "</rss>"
rs.close

沒有留言: