在.Net 平台下的应用中,应用对内(外)的访问往往通过Web Service来实现,当然现在也可以使用WCF。大多数情况下,Web Service都是宿主在IIS上的,但是从SQL Server 2005起,我们可以在SQL Server 2005中发布本机XML Web Service,这样我们可以像访问宿主在IIS上Web Service一样来访问SQL Server的XML Web Service。
注:在SQL Server 2008/R2中,已经不推荐使用本机XML Web Service,将会被WCF或ASP.Net所取代,在本文中只探讨如何定义和使用本机XML Web Service。参见:
http://msdn.microsoft.com/zh-cn/library/cc280436.aspx
虽然在SQL Server的未来版本中,本机XML Web Service将不存在,从技术爱好的角度来看,我们还是有必要了解一下这项技术,因为使用SQL Server 本机XML Web Service可以快速方便的把自定义函数或者存储过程转化为Web服务接口暴露出来。
要在SQL Server中实现本机XML Web Service,需要3个步骤:
1. 创建存储过程(已访问存储过程的Web Service方法为例), Create Procedure
2. 保留命名空间, sp_reserve_http_namespace
3. 创建访问端点,Create Endpoint
下面我们以微软的实例数据库AdventureWorks为例,把访问AddressType表的存储过程转化成Web Service,下面来详细说明。
1.创建存储过程
CREATE PROCEDURE dbo.PersonAddressTypeProc -- Add the parameters for the stored procedure here AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON;
-- Insert statements for procedure here SELECT [AddressTypeID] ,[Name] ,[rowguid] ,[ModifiedDate] FROM [AdventureWorks].[Person].[AddressType] END GO |
2.保留命名空间
sp_reserve_http_namespace N'http://localhost:80/AddressType' |
这里我们将要创建的命名空间是:http://localhost/AddressType,如果没有执行sp_reserve_http_namespace,而去直接创建访问端点,那么会提示命名空间没有被创建或不存在。有关sp_reserve_http_namespace的更多内容可以访问:
http://msdn.microsoft.com/en-us/library/ms190614.aspx
4. 创建访问端点
5. Create Endpoint GetAddressType 6. State = Started 7. AS Http 8. ( 9. path='/AddressType', 10. Authentication =(INTEGRATED), 11. ports =(CLEAR), 12. site ='localhost' 13.) 14.For SOAP 15.( 16. WebMetHod 'AddressTypeList'(Name ='AdventureWorks.dbo.PersonAddressTypeProc'), 17. Batches = disabled, 18. wsdl = default, 19. database ='AdventureWorks', 20. NAMESPACE = 'http://localhost:80/AddressType' 21.) 22. GO |
有关Create Endpoint的参数,感兴趣的读者可以参考:
http://msdn.microsoft.com/zh-cn/library/ms181591.aspx
在本文中,对语句及其参数并不做详细解释,有关详细解释大家可以参考《SQL SERVER 2005本机Web服务支持(实战篇)》,这里就不赘述了。下面给出完整代码:
-- ================================================ -- Template generated from Template Explorer using: -- Create Procedure (New Menu).SQL -- -- Use the Specify Values for Template Parameters -- command (Ctrl-Shift-M) to fill in the parameter -- values below. -- -- This block of comments will not be included in -- the definition of the procedure. -- ================================================ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- ============================================= -- Author: <Author,,Name> -- Create date: <Create Date,,> -- Description: <Description,,> -- ============================================= CREATE PROCEDURE dbo.PersonAddressTypeProc -- Add the parameters for the stored procedure here AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON;
-- Insert statements for procedure here SELECT [AddressTypeID] ,[Name] ,[rowguid] ,[ModifiedDate] FROM [AdventureWorks].[Person].[AddressType] END GO
sp_reserve_http_namespace N'http://localhost:80/AddressType'
Create Endpoint GetAddressType State = Started AS Http ( path='/AddressType', Authentication =(INTEGRATED), ports =(CLEAR), site ='localhost' ) For SOAP ( WebMetHod 'AddressTypeList'(Name ='AdventureWorks.dbo.PersonAddressTypeProc'), Batches = disabled, wsdl = default, database ='AdventureWorks', NAMESPACE = 'http://localhost:80/AddressType' ) GO
|