C# 将 SQL Server 数据库作为 Web 服务公开以从中获取数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2080412/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 23:20:15  来源:igfitidea点击:

Expose SQL Server database as web service to get data from

c#.netsql-serverweb-services

提问by abmv

Is there any .NET tool to expose the data of my tables in Microsoft SQL Server as web services? Do I have to write the code? Are there any samples? What do your recommend as to how to expose the data?

是否有任何 .NET 工具可以将我在 Microsoft SQL Server 中的表数据公开为 Web 服务?我必须写代码吗?有样品吗?您对如何公开数据有什么建议?

采纳答案by Rubens Farias

While to use WCF Data Servicescan be an option, just like Anton said, you should consider if it's a good idea to provide a direct path to your entire/partial database.

虽然使用WCF 数据服务是一种选择,就像Anton 所说的那样,您应该考虑提供到整个/部分数据库的直接路径是否是个好主意。

Another option is to build a data access layer, which will allow just a small operation set, like: "you can to add a customer, but you're not allowed to delete an invoice"

另一种选择是建立一个数据访问层,它只允许一个小的操作集,例如:“您可以添加客户,但不允许删除发票”

回答by Anton

I think you'll want to read up on WCF Data Services, available from .net Framework 3.5 and upwards.

我认为您需要阅读 WCF 数据服务,可从 .net Framework 3.5 及更高版本获得。

回答by Daniel Vassallo

As from SQL Server 2005 you can expose native XML web services directly from the database.

从 SQL Server 2005 开始,您可以直接从数据库公开本机 XML Web 服务。

SQL Server can be configured to listen natively for HTTP SOAP requests through an HTTP endpoint. In general you would want to expose stored procedures or user-defined functions as HTTP endpoints, so a little coding is required. But it should be very easy to follow from the examples.

SQL Server 可以配置为通过 HTTP 终结点本地侦听 HTTP SOAP 请求。通常,您希望将存储过程或用户定义的函数公开为 HTTP 端点,因此需要进行一些编码。但是从示例中应该很容易理解。

You would normally start by creating a stored procedure as follows:

您通常首先创建一个存储过程,如下所示:

CREATE PROCEDURE [dbo].[getContact]
   @ID [int]       
AS
BEGIN
   SELECT * FROM [AdventureWorks].[Person].[Contact] WHERE ContactID = @ID   
END;

And then you would define your HTTP endpoint like this:

然后你可以像这样定义你的 HTTP 端点:

CREATE ENDPOINT SQLEP_GetContact
    STATE = STARTED
AS HTTP
(
    PATH = '/Contact',
    AUTHENTICATION = (INTEGRATED),
    PORTS = (CLEAR),
    SITE = 'localhost'
)
FOR SOAP
(
    WEBMETHOD 'ContactInfo' (NAME='AdventureWorks.dbo.getContact'),
    BATCHES = DISABLED,
    WSDL = DEFAULT,
    DATABASE = 'AdventureWorks',
    NAMESPACE = 'http://AdventureWorks/Contact'
);

After creating the endpoint, you can submit an HTTP request to the server to ensure that the endpoint is responding: http://localhost/Contact?wsdl.

创建端点后,您可以向服务器提交 HTTP 请求以确保端点正在响应:http://localhost/Contact?wsdl

To modify or to stop your endpoint, you can use the ALTER ENDPOINTcommand:

要修改或停止您的端点,您可以使用以下ALTER ENDPOINT命令:

ALTER ENDPOINT SQLEP_GetContact
    STATE = STOPPED;

You may want to proceed by checking out the following articles:

您可能希望继续查看以下文章:

UPDATE:Following Ed Harper's comment below, please note that native XML web services have been deprecated in SQL Server 2008 (November 2009), and this feature will be removed in future version of SQL Server. Microsoft is suggesting using WCF web services instead. Source: MSDN - Native XML Web Services: Deprecated in SQL Server 2008

更新:在下面Ed Harper的评论之后,请注意本机 XML Web 服务已在 SQL Server 2008(2009 年 11 月)中被弃用,此功能将在 SQL Server 的未来版本中删除。Microsoft 建议改用 WCF Web 服务。来源:MSDN - 本机 XML Web 服务:SQL Server 2008 中已弃用

回答by Stephen Oberauer

Scott Hanselman explains how to create an OData / Open Data / WCF Data Service from a database using Visual Studio 2010:

Scott Hanselman 解释了如何使用 Visual Studio 2010 从数据库创建 OData/开放数据/WCF 数据服务:

http://www.hanselman.com/blog/CreatingAnODataAPIForStackOverflowIncludingXMLAndJSONIn30Minutes.aspx

http://www.hanselman.com/blog/CreatingAnODataAPIForStackOverflowIn包括XMLAndJSONIn30Minutes.aspx