简体   繁体   English

Web应用程序到SQL Server的连接未连接(指定服务器/实例时出错)

[英]Web App to SQL Server Not Connecting ( Error Locating Server/Instance Specified)

I know this is an often asked question, but I'm still receiving this error and I have tried: 我知道这是一个经常被问到的问题,但是我仍然收到此错误,并且我已经尝试过:

  • Using localhost, the IP address and the instance name in the connection string 使用本地主机,连接字符串中的IP地址和实例名称
  • Changing the SQL Server Configuration Manager so Named Pipes and TCP are both enabled Ensured the database is set to allow remote connections 更改SQL Server配置管理器,以便同时启用命名管道和TCP确保数据库已设置为允许远程连接
  • Opened 1433 & 1434 for UDP & TCP in Windows Firewall, in fact I tried turning off the firewall for a moment to see if it would connect 在Windows防火墙中针对UDP和TCP打开了1433和1434,实际上我尝试关闭防火墙一会儿以查看它是否可以连接
  • Ensured the browser & the MSSQLSERVER service are both up and running using NETWORK SERVICE 使用网络服务确保浏览器和MSSQLSERVER服务都已启动并正在运行

Now for some oddness. 现在有些奇怪。 The SQL Server instance and the IIS running the website are on the same machine. SQL Server实例和运行网站的IIS位于同一台计算机上。

If I open a browser and browse to the website I get the error. 如果打开浏览器并浏览到网站,则会收到错误消息。 If I open a browser on my local machine and browse to the website I get the error. 如果我在本地计算机上打开浏览器并浏览到网站,则会收到错误消息。 If I change the connection string in my development PC to the IP address of the remote server running SQL server and debug the application it connects and runs without error! 如果我将开发PC中的连接字符串更改为运行SQL Server的远程服务器的IP地址,并调试它连接并运行的应用程序,则不会出错!

I am totally at a loss now as to what could be the error. 现在我完全不知所措,可能是什么错误。

This is SQL Server 2008 RC2. 这是SQL Server 2008 RC2。

I'm on the verge of giving up - I very much appreciate the efforts you guys have made, I have tried all the suggestions here and I am coming to the conclusion that something must have gone awry in the set-up of the server, the website or the database. 我快要放弃了-非常感谢你们付出的努力,我在这里尝试了所有建议,得出的结论是服务器的设置一定有问题,网站或数据库。 Just to reiterate if you remote desktop onto the server and connect to the website using http://gdservers you can login and it all works fine, if you use http://gdservers.<domain>.co.uk the login fails. 只是要重申一下,如果您将远程桌面连接到服务器并使用http://gdservers连接到网站,则可以登录,并且一切正常,如果您使用http://gdservers.<domain>.co.uk则登录失败。 The error in the event log is a membership credentials failure, not blocked by firewall or anything like that. 事件日志中的错误是成员资格凭证失败,未被防火墙或类似原因阻止。 If there's any other suggestions I'd love to hear them, thanks again. 如果还有其他建议,我想听听他们的意见,再次感谢。

I don't think anyone spotted it and I only spotted it on Wednesday and then realised I'd made another error. 我认为没有人发现它,我只在星期三发现它,然后才意识到我犯了另一个错误。 If someone has mentioned it above let me know and I'll change the award and give them the points. 如果有人在上面提到它,请告诉我,我将更改奖励并给他们积分。

The applicationName was missing from the membership provider. 成员资格提供程序缺少applicationName。 I spotted it on Wednesday and added one in, what I didn't realise I'd done is made a spelling mistake when adding the applicationName in, so when I did this, it didn't fix it, so I thought it wasn't the problem. 我在星期三发现它并添加了一个,但我没有意识到自己做的是在添加applicationName时犯了一个拼写错误,因此当我这样做时,它并没有解决它,所以我认为不是这样。这个问题。

Yesterday whilst reviewing the code I noticed I spelt the applicationName 'GSServers' instead of 'GDServers' when I changed it, it all started working. 昨天,在查看代码时,我发现我在更改应用名称时将其拼写为“ GSServers”而不是“ GDServers”,所有这些都开始起作用。

Not a very satisfactory conclusion and I apologise. 结论不是很令人满意,我深表歉意。

Thank you to everyone who tried to solve this problem and sticking with it. 感谢所有试图解决这个问题并坚持下去的人。

As I said if someone did mention this in an answer or comment somewhere let me know and they can have the points. 正如我说过的,如果有人在某个地方的回答或评论中提到了这一点,请告诉我,他们可以指出要点。

If your web app intends to use SQL Server Authentication with a SQL login/password then make sure that your connection string explicitly states Integrated Security=False . 如果您的Web应用程序打算将SQL Server身份验证与SQL登录名/密码一起使用,请确保您的连接字符串明确声明Integrated Security=False Otherwise your connection may default to Windows Authentication and not behave as expected. 否则,您的连接可能默认为Windows身份验证,并且无法正常运行。

Edit 编辑

Also, be sure that the connection string you're tweaking is the one that is actually being used to make the connection. 另外,请确保您要调整的连接字符串是实际用于建立连接的字符串。 Sometimes things can get confusing when an application has multiple components that may have their own configuration settings. 有时,当应用程序具有可能具有自己的配置设置的多个组件时,事情会变得令人困惑。 For example, the problem in the thread here turned out to be: 例如, 这里线程中的问题原来是:

I was updating the connection string on the web.config but, it's a multitier application, the data access component is in a separate .dll and has its own connection string. 我正在更新web.config上的连接字符串,但它是一个多层应用程序,数据访问组件位于单独的.dll中,并具有自己的连接字符串。

Edit 编辑

I'm beginning to wonder if the SQL Membership Provider is using Windows Authentication to connect to the SQL Server even if your data connections aren't. 我开始怀疑SQL成员资格提供程序是否正在使用Windows身份验证连接到SQL Server,即使您的数据连接不是。 Check out the video here and perhaps review your settings (especially the authentication setting in the provider setup steps). 此处查看视频并可能查看您的设置(尤其是提供商设置步骤中的身份验证设置)。

Comment: Using SSPI with a windows user account is preferable from a security perspective. 注释:从安全角度来看,最好将SSPI与Windows用户帐户一起使用。

Answer: 回答:

Port 1433 is used for SSL by the default (unnamed) instance. 默认(未命名)实例将端口1433用于SSL。 Ensure that your named instance has an SSL port properly assigned: 确保命名实例具有正确分配的SSL端口:
http://blogs.msdn.com/b/sqlexpress/archive/2005/05/05/415084.aspx http://blogs.msdn.com/b/sqlexpress/archive/2005/05/05/415084.aspx

Try these connect strings (note that they don't use 'localhost' - which is case sensitive, I believe): 尝试以下连接字符串(请注意,它们不使用“ localhost”-我认为这是区分大小写的):

  • 'Provider=SQLOLEDB;Data Source=COMPUTERNAME\\SQLEXPRESS;Initial Catalog=Test;User ID=COMPUTERNAME\\USERNAME; 'Provider = SQLOLEDB;数据源= COMPUTERNAME \\ SQLEXPRESS;初始目录=测试;用户ID = COMPUTERNAME \\ USERNAME; Password=PASSWORD;Integrated Security=False' 密码=密码;集成安全性=假'

  • 'Provider=SQLOLEDB;Data Source=.\\SQLEXPRESS;Initial Catalog=Test;User ID=COMPUTERNAME\\USERNAME; 'Provider = SQLOLEDB;数据源=。\\ SQLEXPRESS;初始目录=测试;用户ID = COMPUTERNAME \\ USERNAME; Password=PASSWORD;Integrated Security=False' 密码=密码;集成安全性=假'

  • 'Provider=SQLOLEDB;Data Source=.\\SQLEXPRESS;Initial Catalog=Test;User ID=COMPUTERNAME\\USERNAME; 'Provider = SQLOLEDB;数据源=。\\ SQLEXPRESS;初始目录=测试;用户ID = COMPUTERNAME \\ USERNAME; Password=PASSWORD;Integrated Security=SSPI' 密码= PASSWORD;集成安全性= SSPI'

ADDITIONAL EDIT: 其他编辑:

Whenever client is run on same machine as SQL Server, the error occurs. 只要客户端与SQL Server在同一台计算机上运行,​​就会发生错误。 When client is run from dev machine, no error. 从开发机运行客户端时,没有错误。 You may be failing to resolve the machine name because DNS does not support any of the names. 您可能无法解析计算机名称,因为DNS不支持任何名称。

Suggestion: Connect using localhost. 建议:使用本地主机连接。 Ensure you can ping localhost and/or telnet localhost 1433 . 确保您可以ping localhost和/或telnet localhost 1433 Ensure your hosts file (eg C:\\Windows\\System32\\drivers\\etc\\hosts) has entry: 确保您的主机文件(例如C:\\ Windows \\ System32 \\ drivers \\ etc \\ hosts)具有条目:

  127.0.0.1       localhost

Alternatively, use: (local)\\INSTANCENAME 或者,使用:(本地)\\ INSTANCENAME

Run SQLCMD –L to list installations (confirming correct browser operation). 运行SQLCMD –L列出安装(确认浏览器正确运行)。

Ensure you don't have older versions of the management tools ensured on the same server. 确保在同一服务器上没有旧版本的管理工具。 Eg 2008 & 2005 tools cannot coexist - but 2008 tools are backwards compatible, so you can delete both and just cleanly install 2008. 例如,2008和2005工具不能共存-但是2008工具是向后兼容的,因此您可以删除这两个工具并干净地安装2008。

Stupid question but, have you checked the credentials of the login? 愚蠢的问题,但是,您是否检查了登录凭据? I'm still very much a noob in this area but I found out thru backing up and restoring a database (at least in MS SQL Server) that the restored user login doesn't work. 我在这方面仍然是一个菜鸟,但是我发现通过备份和还原数据库(至少在MS SQL Server中),还原的用户登录无效。

I've had to delete the user from the database and add it back in, then everything works fine. 我不得不从数据库中删除用户并将其重新添加,然后一切正常。 Could it be you are accessing a different version of the database on the non-working machine? 难道您正在非运行机器上访问数据库的其他版本?

How are you indicating the Data Source/server name in your connection string? 您如何在连接字符串中指示数据源/服务器名称? I've encountered situations where there was a hosts file on the server redirecting elsewhere, or the server was indicated by a domain name which resolved to the external IP address of the server, or other similar oddness. 我遇到过这样的情况,服务器上有一个主机文件重定向到其他地方,或者服务器由解析为服务器的外部IP地址的域名或其他类似的奇数表示。

Also, if you have SQL Serve installed on the server, do you also have Management Studio installed there? 另外,如果您在服务器上安装了SQL Serve,是否还在其中安装了Management Studio? It's the quickest way to rule out code or permission errors, just trying variations through Management Studio until you find login details that work (or know that none do). 这是排除代码或权限错误的最快方法,只需尝试通过Management Studio进行变体,直到找到有效的登录详细信息(或知道不起作用)。

Usually the easiest and most secure way is using integrated security: 通常,最简单,最安全的方法是使用集成安全性:

<connectionStrings>
    <add name="HitCounterContext"
         connectionString="Data Source=(local)\SQLEXPRESS;Initial Catalog=db;Integrated Security=True"
         providerName="System.Data.SqlClient" />
</connectionStrings>

Make sure you use the correct instance name, which per default is "SQLExpress". 确保使用正确的实例名称,默认情况下为“ SQLExpress”。 I don't know how IDEs work with that, but using this connection string with IIS should at least connect successfully (which is not equal to authentication). 我不知道IDE如何使用它,但是在IIS中使用此连接字符串至少应成功连接(这不等于身份验证)。

Maybe the authentication fails because your database must have a user for the app pool account. 身份验证可能会失败,因为您的数据库必须具有该应用程序池帐户的用户。 If you disable custom errors in your web.config so you get .net error messages, the error message should tell you that the authentication failed for the app pool user (the message actually tells you which user is used). 如果在web.config禁用了自定义错误,因此您收到.net错误消息,则该错误消息应告诉您应用程序池用户的身份验证失败(该消息实际上告诉您使用了哪个用户)。 For instance: If you create a new website called "yoursite.com" using IIS 7, per default it creates a new app pool with the name "yoursite.com". 例如:如果您使用IIS 7创建一个名为“ yoursite.com”的新网站,则默认情况下,它将创建一个名为“ yoursite.com”的新应用程序池。 With that you'll have to add a user to the database for IIS APPPOOL/yoursite.com (might not be in the users list) and let it be the db_owner for testing. 这样,您必须将一个用户添加到IIS APPPOOL/yoursite.com的数据库中(可能不在用户列表中),并使其成为进行测试的db_owner

Add the user for the app pool using "SQL Server Management Studio" like that (sorry it's german. This actually is the form after the user was added, but it should look the same while adding): 像这样使用“ SQL Server Management Studio”为应用程序池添加用户(很抱歉,它是德语。这实际上是添加用户后的表格,但是添加时看起来应该一样):

添加应用程序池用户

After that, your users list should look like 之后,您的用户列表应如下所示

数据库用户列表

and the servers login list like 和服务器登录列表,例如

服务器登录列表

where each app pool has its own login for the server and user for the respective database. 其中,每个应用程序池都有各自的服务器登录名和相应数据库的用户名。

By the way: Connecting from the Management Studio works just the same like from your asp.net app, except that you normally use your current user with SMS: 顺便说一句:从Management Studio进行连接的方式与从asp.net应用程序进行的方式相同,所不同的是,您通常将当前用户与SMS结合使用:

在此处输入图片说明

So, if this works, the connection from your app should do as well. 因此,如果可行,来自您应用程序的连接也应该起作用。 The rest relies on the user for the app pool. 其余部分依赖用户使用应用程序池。

The fact that it's trying to login as sa locally suggests you're not using the connection string in your code; 它试图在本地以sa身份登录的事实表明您未在代码中使用连接字符串; I don't think it's a SQL server problem, but an app problem. 我不认为这是SQL Server问题,而是应用程序问题。

In your app, make sure you're actually using the value of the connection string. 在您的应用中,请确保您实际上使用的是连接字符串的值。 Don't use the default constructor. 不要使用默认的构造函数。 Eg 例如

using(SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["myconnectionstring"].ConnectionString)){ /* .. code */ }

not

using(SqlConnection conn = new SqlConnection()){ /* .. code .. */ }

If I change the connection string in my development PC to the IP address of the remote server running SQL server and debug the application it connects and runs without error! 如果我将开发PC中的连接字符串更改为运行SQL Server的远程服务器的IP地址,并调试它连接并运行的应用程序,则不会出错!

It's possible you're dev box has been added an authenticated login, and it's automagically "working". 有可能您为开发人员框添加了经过身份验证的登录名,并且自动“工作”。

暂无
暂无

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

相关问题 连接数据库ASP .NET MVC 5 SQL网络接口时出现问题,错误:26-指定服务器/实例时出错 - Issue connecting to databases ASP .NET MVC 5 SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified 使用C#的SQL上的错误26,指定了错误定位服务器/实例 - Error 26 on SQL with C#, error locating server / instance specified SQL错误:26-查找指定的服务器/实例时出错 - SQL error:26 - Error Locating Server/Instance Specified 查找指定的服务器/实例时出错| 视觉工作室 - Error locating server/instance specified | Visual Studio .NET Core WebAPI 应用程序在本地连接,但在服务器上失败,并出现 SQL 网络接口错误:26 定位服务器/指定实例时出错 - .NET Core WebAPI app connects locally but fails on the Server with SQL Network Interfaces error: 26 Error Locating Server/Instance Specified SQL服务器错误(提供程序:SQL网络接口,错误:26-错误指定服务器/实例的位置) - SQL SERVER ERROR (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified) 将ASP.NET MVC与数据库连接,错误:26-查找指定的服务器/实例时出错 - Connecting ASP.NET MVC with Database, error: 26 - Error Locating Server/Instance Specified 错误:26-托管到Web服务器后定位服务器/实例时出错 - Error: 26 - Error Locating Server/Instance Specified after hosting to a web server SQL Server EXPRESS:错误:26-在安装后指定服务器/实例时出错 - SQL Server EXPRESS: Error: 26 - Error Locating Server/Instance Specified after setup 错误26:错误定位生产环境中指定的服务器/实例-SQL Server 2014 WPF WCF - Error 26: Error Locating Server/Instance Specified in production environment - SQL server 2014 wpf wcf
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM