.net - 使用 Windows Azure 托管的连接问题

标签 .net sql-server exception azure

当我从 ASP.NET MVC 应用程序连接到 SQL Azure 时,出现以下错误。

Exception Details: System.Data.SqlClient.SqlException: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)

但是,我能够从 SQL Server Management Studio 成功连接到 SQL Azure。我还尝试使用 sqlCMD 连接,并且连接没有任何问题。唯一出现的问题是在运行时出现此异常。

最佳答案

您的连接字符串肯定有问题。您无法通过命名管道连接到 Windows Azure SQL 数据库(也称为 SQL Azure)。唯一支持的协议(protocol)是 TCP。所以你的 conn 字符串看起来像这样:

Server=tcp:[servername].database.windows.net,1433;Database=[db_name];User ID=[username]@[servername];Password=[password];Trusted_Connection=False;Encrypt=True;Connection Timeout=30

您可以从管理门户获取连接字符串。当您转到数据库时,右侧会显示连接字符串链接(新门户):

SQL Azure show connection strings (new portal)

和旧的传送门几乎一样:

SQL Azure Connection Strings old portal

最后,您必须在防火墙规则中启用“允许其他 Windows Azure 服务连接到此服务器”。

**** 根据下面的评论更新 ****

这个连接字符串条目在哪里?在 web.config 中,或在 web.release.config/web.debug.config 中。请注意,web.config 转换在开发时不起作用,仅在打包时起作用!这个错误是本地调试的时候出现的,还是部署的时候出现的?

如果在本地,您可以检查您实际使用的连接字符串吗?

请注意,有默认的“ApplicationServices”连接字符串(位于 apphost.config 中),当您尝试在默认 MVC 应用程序中“登录/注册”时可能会发生这种情况!该连接字符串假定本地 SQL Express 实例存在并正在运行。这是成员(member)身份和角色提供者的默认连接字符串。除非您更改成员(member)资格提供商设置,否则您将无法登录/注册以及任何需要身份验证的操作。

关于.net - 使用 Windows Azure 托管的连接问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11464886/

相关文章:

.net - 如何将字节数组转换为位图实例 (.NET)?

.net - Visual Studio 2008 的性能类似于 VBA

c# - 亚马逊 MWS 获取产品价格 .NET 库 C#

存储在 Microsoft SQL 中的 Python blob PDF - 转换回 PDF

php - PHP 可以与 MS SQL 数据库一起使用吗

java - 如何在 Spring 中实现 GraphQL 的异常处理程序

java - 在 RunTimeException 的情况下如何在子类中声明父异常?

java - 尝试在 ArrayList 中遍历时如何修复 java.util.ConcurrentModificationException 错误

c# - 安装Windows服务时是否需要手动创建Windows事件日志源

sql-server - 发生小数点四舍五入时引发错误