Share via

Cannot connect to SQL Server Express database from an UWP app

Preveen 256 Reputation points
2020-07-31T15:26:24.397+00:00

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.

Azure SQL Database
Developer technologies | Universal Windows Platform (UWP)

3 answers

Sort by: Most helpful
  1. Ivanich 306 Reputation points
    2020-08-06T19:30:24.337+00:00

    If client app and SQL Server instance are working on the same machine, it could be loopback issue. You may try following command:

    checknetisolation loopbackexempt -a -n=package_family_name
    

    I'm not sure whether this is still true for recent versions of Windows 10, but old versions used to deny connections to localhost.

    Was this answer helpful?

    0 comments No comments

  2. Anita Basa 1 Reputation point
    2020-08-05T08:19:56.92+00:00

    Please try to test connection with non-UWP tools.

    Was this answer helpful?


  3. Vladislavas Petkevic 1 Reputation point
    2020-08-04T09:47:05.333+00:00

    Hello,
    if client and SQL Express installed on different machines
    I suppose SQL express is not listening on port 1433 (by default only local requests supported)

    You have to run SQL server configuration manager and Enable TCP/IP in SQL server network configuration
    and restart SQL express (or entire PC/server where you have installed it)

    to check port connectivity, open SMSS on another PC and connect to server:

    tcp:Servername, 1433

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.