简体   繁体   English

WCF服务和localhost SQL Server的连接字符串是什么

[英]What is the connection string for WCF service and localhost SQL Server

I have seen bits and pieces related to this on the web but I still cannot figure it out. 我已经在网上看到了与此相关的点点滴滴,但我仍然无法弄清楚。 I am hosting a WCF service in IIS and on the same server I have SQL Server 2012 installed. 我在IIS中托管WCF服务,并且在安装SQL Server 2012的同一服务器上。 I would like to have the WCF service to CRU&D operations on the SQL Server database. 我想使用WCF服务来对SQL Server数据库进行CRU&D操作。

I have read that because I am creating the connection string in a WCF service I need to treat it as a remote SQL Server instance (despite being on the same server). 我读到这是因为我在WCF服务中创建连接字符串,因此需要将其视为远程SQL Server实例(尽管在同一服务器上)。 However, I am using integrated security with a domain user. 但是,我正在与域用户一起使用集成安全性。 The best answers I have seen to this are: 我所看到的最好的答案是:

  1. Create a local user and use a remote connection string with that local user account: Windows Service unable to connect to SQL Server on localhost 创建本地用户并使用该本地用户帐户使用远程连接字符串: Windows服务无法连接到本地主机上的SQL Server

  2. Use a remote SQL Server instance (I guess create a whole other SQL Server). 使用远程SQL Server实例(我想创建一个其他SQL Server)。

Outside of those though, I am lost. 除了那些之外,我迷路了。 I'm wondering if there is maybe another option. 我想知道是否还有其他选择。 Possibly one that keeps my domain user account and creates a proper connection string? 可能会保留我的域用户帐户并创建正确的连接字符串吗? Any suggestions would be greatly appreciated. 任何建议将不胜感激。

Thank you all by the way! 谢谢大家!

Well, you have not said what the issue is with the existing connection strings. 好吧,您没有说出现有连接字符串的问题所在。 So let me explore all possibilities in this answer -- 因此,让我探索这个答案中的所有可能性-

  1. Since you are using domain users to connect to your database, you would need to add either a domain/local Windows User Group to your SQL Server under Logins to permit that set of users to connect to your server. 由于您正在使用域用户连接到数据库,因此您需要在“登录”下向SQL Server添加域/本地Windows用户组,以允许该组用户连接到服务器。 By default, everyone is denied access unless added explicitly. 默认情况下,除非明确添加,否则拒绝所有人。

  2. Next, your connection string is of the format that [J0e3gan] has already posted. 接下来,您的连接字符串具有[J0e3gan]已发布的格式。 In the web.config file of your WCF service, ensure you have something like this -- 在WCF服务的web.config文件中,确保您具有以下内容-

     <connectionStrings><add name="DbConnection" connectionString="Data Source=(local);Initial Catalog=MyDataase;Persist Security Info=False;Integrated Security=SSPI;" providerName="System.Data.SqlClient" /></connectionStrings> 
  3. In IIS, set the "Identity" being used for your App Pool for the WCF Service to be the same as the domain user you want to use to authenticate/authorize the WCF - Database connection. 在IIS中,将用于WCF服务的应用程序池中使用的“身份”设置为与要用于身份验证/授权WCF-数据库连接的域用户相同。

Let me know if you need further info. 让我知道您是否需要更多信息。

There is no connection-driven need to use a remote SQL Server instance or ditch Windows authentication in your connection string. 没有连接驱动的需要在您的连接字符串中使用远程SQL Server实例或Windows身份验证。 An IIS-hosted WCF service can work fine with both. IIS托管的WCF服务可以与这两者一起正常工作。

Your connection string should look something like this: 您的连接字符串应如下所示:

<add name="DbConnection" connectionString="Data Source=.;Initial Catalog=MyDatabase;Persist Security Info=False;Integrated Security=SSPI;" providerName="System.Data.SqlClient"/>

Also, you may find the ConnectionStrings.com SQL Server 2012 page helpful. 此外,您可能会发现ConnectionStrings.com SQL Server 2012页面很有帮助。 Over the years I have found ConnectionStrings.com invaluable for reality checking connection-string variations for different versions of SQL Server, different clients etcetera. 多年来,我发现ConnectionStrings.com对于现实检查不同版本的SQL Server,不同客户端等的连接字符串变体非常有价值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM