简体   繁体   中英

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. 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.

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.
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: TCP Provider, error: 40 - Could not open a connection to SQL Server)

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.

The client and my dev box are both version 2004.

Package.appxmanifest file has the following capabilities checked:

  • Enterprise Authentication
  • Internet (Client & Server)
  • Internet (Client)
  • Private Networks (Client & Server)

Windows Firewall has the correct ports open. In SSMS, I have enabled allowing remote connections, enabled 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.

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. 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.

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 ).

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

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.

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. In the end, I finally got it working by running the following command

checknetisolation loopbackexempt -a -n=YourAppPackageName

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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