通过HTTP访问SQL Server 2000数据库

来源:互联网 发布:n86软件无法安装 编辑:程序博客网 时间:2024/05/02 00:00
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
Microsoft SQL Server 2000的新特性之一就是支持XML,所以用户可以HTTP://www.66of.com" target=_blank>通过HTTP方式直接访问具备XML功能的SQL Server2000数据库。下面介绍实现方法。

系统环境:Windows2000 Server(包含IIS)+ MS SQL Server2000

数据库SQL Server2000附带的经典数据库样本:NorthWind

一、配置IIS和SQL Server virtual domains;
1.新建文件目录

打开Windows资源管理器,在Inetpub/wwwroot下新建目录northwind:

C:/Inetpub/wwwroot/northwind

在northwind目录下再新建2个子目录:template和schema:

C:/Inetpub/wwwroot/nwind/template,用于放置XML模版;

C:/Inetpub/Wwwroot/nwind/schema,用于放置schema文件。

2.映射IIS的虚拟目录

SQL程序组中选择“Configure SQL XML Support in IIS”,在默认站点下选择“新建”->“虚拟路径”;然后进行五步标准配置。

(1)在“General”页中,输入虚拟路径名称“northwind”并映射至实际硬盘路径,结果见图。


(2)在“Security”页中,选择相应的登陆帐号和口令。本例中使用系统帐号sa,结果见图。

(3)在“Data Source”页中,选择SQL Server名称或IP地址,并选择数据库,结果见图。

(4)在“Settings”页中,分别选中“允许URL查询”、“允许模版查询”、“允许XPath”和“允许Post”,结果见图。

(5)在“Virtual Names”页中,分别定义Template、Schema和Dbobject,并指定实际硬盘路径,结果见图。

然后,确定并关闭窗口。结果如图。

二、用URL方式执行查询和存储过程;
至此,我们已经设置好SQL和IIS,下面开始精彩的访问历程。先在IE浏览器中键入以下地址:

HTTP://172.24.2.98/northwind?SQL=select+*+from+customers+for+xml+auto&root=root

得到结果如图所示。


其中,

“northwind”为虚拟路径名;

“select+*+from+customers”为SQL查询语句“Select * from customers”

“for XML”语句表示输出的数据格式为XML文档,而非标准数据行。

“root”参数表示为顶层元素。

RAW、AUTO和EXPLICIT三种XML模式分别为:

RAW在每条记录前加一个简单的标识,如“<rowCustomerID="ALFKI"…”

AUTO则会在每条记录前加入数据表的名称,如“<CustomersCustomerID="ALFKI"”

EXPLICIT则可用于定义XML的树形结构。

初试成功后,我们再尝试使用URL地址执行存储过程(Stored Procedure)。先在SQL Query Analyzer中新建一个名为CategoryInfo的存储过程;代码如下:

CREATE PROCEDURE CategoryInfo
AS
SELECT CategoryName, Description FROM Categories
FOR XML AUTO

HTTP://www.66of.com" target=_blank>通过地址

HTTP://172.24.2.98/northwind?SQL=execute+categoryinfo&root=root

即可执行存储过程。

三、访问XML模版文件
此外,网站开发人员还可以定义XML的模版,定义查询条件和结果输出格式:

如,在wwwroot/northwind/template目录下生成一个XML文档first.xml,其中包含一个查询语句,代码如下:

<root xmlns:SQL="urn:schemas-microsoft-com:xml-SQL" >
  <SQL:query>
    SELECT *
    FROM Customers AS Customer
    FOR XML auto
  </SQL:query>
</root>

HTTP://www.66of.com" target=_blank>通过地址HTTP://172.24.2.98/northwind/template/first.xml访问,得到结果。

结合Extensible Stylesheet Language (XSL)可以格式化查询结果,使报表更加美观。

HTTP://www.66of.com" target=_blank>通过以上介绍,你也许已经对SQL2000和XML有了初步的了解。仔细阅读SQL的帮助文件和MSDN.COM会对你深入掌握SQL2000有着更大的帮助。



<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击