在.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
|