简体   繁体   English

无法从 UWP 应用程序连接到 SQL Server Express 数据库

[英]Cannot connect to SQL Server Express database from an UWP app

I have a UWP app that needs to connect to a SQL Server Express database.我有一个需要连接到 SQL Server Express 数据库的 UWP 应用程序。 On my dev box, I don't have any issues.在我的开发盒上,我没有任何问题。

On the client PC, I installed SQL Server Express, created the database, setup the users and logins.在客户端 PC 上,我安装了 SQL Server Express,创建了数据库,设置了用户和登录名。

When I run the app, and try to connect to the database, I get an error:当我运行应用程序并尝试连接到数据库时,出现错误:

A network-related or instance-specific error occurred while establishing a connection to SQL Server.与 SQL 服务器建立连接时发生与网络相关或特定于实例的错误。
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.验证实例名称是否正确,并且 SQL 服务器配置为允许远程连接。
(provider: TCP Provider, error: 40 - Could not open a connection to SQL Server) (提供者:TCP 提供者,错误:40 - 无法打开与 SQL 服务器的连接)

The code I'm using to connect is:我用来连接的代码是:

...
using (SqlConnection connection = new SqlConnection(ConnectionStringTextBox.Text.Trim()))
{
    try
    {
        connection.Open();
        MessageBox.Show("Connected successfully!");
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}
...

Using this same code on a desktop app, on the client machine, the connection succeeds.在客户端计算机上的桌面应用程序上使用相同的代码,连接成功。

I have set up the UWP app to use version 1809 of Windows 10.我已将 UWP 应用程序设置为使用 Windows 10 的 1809 版。

The client and my dev box are both version 2004.客户端和我的开发盒都是 2004 版。

Package.appxmanifest file has the following capabilities checked: Package.appxmanifest文件检查了以下功能:

  • Enterprise Authentication企业认证
  • Internet (Client & Server)互联网(客户端和服务器)
  • Internet (Client)互联网(客户端)
  • Private Networks (Client & Server)专用网络(客户端和服务器)

Windows Firewall has the correct ports open. Windows 防火墙打开了正确的端口。 In SSMS, I have enabled allowing remote connections, enabled TCP.在 SSMS 中,我启用了允许远程连接,启用了 TCP。

I don't know what I'm doing wrong or what is going wrong.我不知道我做错了什么或出了什么问题。 This same UWP app works great on my dev box.同样的 UWP 应用程序在我的开发盒上运行良好。

Any advice is appreciated.任何建议表示赞赏。

If you're trying to connect to a SQL Server instance on the same machine it won't work because UWP applications are sandboxed and cannot connect to localhost over TCP/UDP - while connections to remote machines will succeed.如果您尝试连接到同一台机器上的 SQL 服务器实例,它将无法工作,因为UWP 应用程序已被沙盒化,无法通过 TCP/UDP 连接到 localhost - 而与远程机器的连接将成功。 I think this is stupid, but it's By Design .我认为这很愚蠢,但这是设计使然

As a workaround, you can configure SQL Server to use Shared Memory or Named Pipes and those approaches should work.作为一种解决方法,您可以将 SQL 服务器配置为使用共享 Memory 或命名管道,这些方法应该可以工作。

Update:更新:

I forgot that there is now a Capability you can add to your AppX manifest to enable local-loopback connections (only for TCP, it doesn't work for UDP ).我忘现在有一个功能可以添加到您的 AppX 清单中以启用本地环回连接(仅适用于 TCP, 它不适用于 UDP )。

You can try this example on how to connect to sql server in uwp app, and try to do the same steps on your client pc, hope it works: https://docs.microsoft.com/en-us/windows/uwp/data-access/sql-server-databases#first-set-up-your-solution您可以尝试此示例,了解如何在 uwp 应用程序中连接到 sql 服务器,并尝试在您的客户端 PC 上执行相同的步骤,希望它有效: httpsen///umicrosoft.com//数据访问/sql-server-databases#first-set-up-your-solution

I tried everything I could find online, change sql server firewall settings, sql server tcp/ip settings, change sql server browser firewall settings, none of them worked for me.我尝试了所有我可以在网上找到的东西,更改 sql 服务器防火墙设置,sql 服务器 tcp/ip 设置,更改 sql 服务器浏览器防火墙设置,它们都不适合我。

I did read someone saying about turn on loopback capabilities in UWP, but I didn't see that option for the targeted window version I had.我确实读过有人说在 UWP 中打开环回功能,但我没有看到我拥有的目标 window 版本的选项。 In the end, I finally got it working by running the following command最后,我终于通过运行以下命令让它工作了

checknetisolation loopbackexempt -a -n=YourAppPackageName

在此处输入图像描述

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

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