SQL Server静态页面导出技术4


本段文章节选自铁道出版社新出的《用BackOffice建立Intranet/Extranet应用》一书(现已在海淀图书城有售)。本书详尽地讲述了如何使用微软BackOffice系列产品来组建Intranet/Extranet应用。 通过它您将掌握NT的安装和设置、使用IIS建立Web站点、通过ILS建立网络会议系统、用Exchange建立企业的邮件和协作系统、用SQL Server建立Web数据库应用、用Proxy Server建立同Internet安全可靠的连接、用Media Server建立网络电视台/广播站、用Chart server建立功能强大的聊天室、用Site Server建立个性化的邮件列表和分析网站的访问情况、用Commerce Server建立B2B或B2C的电子商务网站。此外本书还对网络的安全性进行了讨论,从而指导您建立一个更为健壮和安全的网络应用。阅读本书之后,您将发现实现丰富多彩的网络应用原来这样简单……
绝对原创,欢迎转载。但请务必保留以上文字。

use test
go
declare
@riqi varchar(20),
@filepath varchar(255),
@listfile varchar(255),
@command varchar(255)
set @riqi=left(convert(varchar(40),getdate(),20),10)
set @filepath='d:\webout'+@riqi+''
set @command='md '+@filepath
execute master.dbo.Xp_cmdshell @command
set @command='md '+@filepath+'images'
execute master.dbo.Xp_cmdshell @command
set @command ='copy d:\test\files\. d:\webout'+@riqi+''
execute master.dbo.Xp_cmdshell @command
set @command ='copy d:\test\files\images\. d:\webout'+@riqi+'\images'
execute master.dbo.Xp_cmdshell @command
set @command ='copy d:\test'+@riqi+'\. d:\webout'+@riqi+''
execute master.dbo.Xp_cmdshell @command
set @listfile=@filepath+'list.htm'
execute sp_makewebtask
@outputfile=@listfile,
@query='select distinct banmian
from gaojian
where kanwu=''出版报'' and datepart(yy,riqi)=datepart(yy,getdate()) and datepart(dy,riqi)=datepart(dy,getdate())',
@templatefile='d:\test\list.tml',
@codepage=936
在此段代码中先定义了一些变量,用来调用存贮过程时使用。其中@riqi变量用于存放当日的日期(其格式为yyy-mm-dd);@filepath变量用于存放产生静态页面的路径;@listfile变量用于存放版面列表页面文件的路径和文件名;@command变量用于存放要执行的系统命令。
随后我们对各个变量进行赋值。并调用xp_cmdshell存贮过程来完成建立相应目录、拷贝文件等工作。xp_cmdshell存贮过程是一个用来执行NT系统命令的扩展存贮过程。其语法结构如下:
xp_cmdshell {'command_string'} [, no_output]
其中command_string参数为要执行的系统命令。而选项no_output则用来指明不输出系统命令的执行结果。
在此段代码的最后,执行未指明whentype参数的sp_makewebtask存贮过程,导出当日的版面列表页面文件。使用的模板文件为list.tml。list.tml文件的代码如下:

1<html>
2<head><title>出版报</title></head>
3<body background="images/WB00703_.gif">
4<script>   
5var t=0;   
6</script>
7<table align="CENTER" border="0">   

begindetail

1<tr>
2<td><img height="14" src="images/Yellowb2.gif" width="14"/>
3<script>   
4var t=t+1;   
5document.write('<a HREF="');   
6document.write(t);   
7document.write('.htm" TARGET="show"><b><i><font SIZE="+1">')   
8</script>   

insert_data_here

enddetail

1</table></body></html>

可以看到,静态页面导出使用的模板文件同IDC技术中使用的htx文件十分相似。其中也包含begindetailenddetail字段。所不同的是,模板文件中不使用字段名来标识字段。只是简单的使用insert_data_here来指明在何处插入结果集中的数据。如果结果集记录中包含多个字段的话,``` insert_data_here

 1在此模板文件中有一段Java程序,其用途是为每个版面按照其顺序产生超链接。其链接分别为1.htm~n.htm,n值为当日版面的数目。   
 2至此我们已经成功地建立了存放页面文件的目录、完成了相应文件的拷贝工作、导出了当日版面的列表文件。下面将为每个版面来产生文章列表页面文件。   
 3declare   
 4@lists int,   
 5@banmian varchar(64),   
 6@filename varchar(64),   
 7@search varchar(2000)   
 8set @lists=0   
 9declare point cursor for   
10select distinct banmian   
11from gaojian   
12where kanwu='出版报' and datepart(yy,riqi)=datepart(yy,getdate()) and datepart(dy,riqi)=datepart(dy,getdate())   
13for read only   
14  
15open point   
16fetch point into   
17@banmian   
18while (@@fetch_status=0)   
19begin   
20set @lists=@lists+1   
21set @filename=@filepath+convert(varchar(64),@lists)+'.htm'   
22set @search='SELECT id,timu,laiyuan   
23FROM gaojian   
24WHERE datepart(yy,riqi)=datepart(yy,convert(datetime,'''+@riqi+'''))   
25and datepart(dy,riqi)=datepart(dy,convert(datetime,'''+@riqi+'''))'+   
26'and banmian ='''+@banmian+'''and kanwu=''出版报''order by timu'   
27execute sp_makewebtask   
28@outputfile=@filename,   
29@query=@search,   
30@templatefile='d:\test\list2.tml',   
31@codepage=936   
32fetch point into   
33@banmian   
34end   
35close point   
36deallocate point   
37在此段代码中我们使用了游标。在此之前我们所使用的SQL语句都是用于集合操作的。也就是说,语句只是用来产生结果集合,或对结果集合进行分组。而要想分别对每个返回的结果记录进行不同的处理,就只有通过游标来实现了。
Published At
Categories with 数据库类
Tagged with
comments powered by Disqus