UWP App not connecting to sql express local databse

Ludwig Nel 21 Reputation points
2022-06-17T11:14:24.047+00:00

Good day.

I realize this question has been asked before.
But non of those answer help.

I have developed a simple UWP app. It connects to an sql database and displays some data.
So this works fine on my Windows 11 development laptop.

Howeever once I installed it on the client machine(Sideloaded) it wont connect to the SQL Express sdatabse on the same pc.
This machine runs Windows 11 Pro and SQL Server Express 2019 is installed.

I have enabled TCP amd Named Pipes in the SQL Server configuration Manager.
I also made sure remote connections are enabled.

As far as I am aware this should do the trick.
But I get the "network related or instance specific error....." exception.

I created a windows forms app to just test the connection and that works find.
This just creates an instance of the SQLConnection object and executes Open on that.
So nothing complicated.

Is there perhaps something else that I am missing here?

O and there is no Anti Virus or Firewall blocking SQL connections

Universal Windows Platform (UWP)
SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,759 questions
Windows 11
Windows 11
A Microsoft operating system designed for productivity, creativity, and ease of use.
8,227 questions
{count} votes

Accepted answer
  1. Roy Li - MSFT 32,051 Reputation points Microsoft Vendor
    2022-06-20T02:30:24.573+00:00

    Hello,

    Welcome to Microsoft Q&A!

    So you are trying to connect to the local host for the to sql express local database, right. Have you enabled the local loopback on your device when you sideloaded your app? UWP apps are isolated and running in the sandbox, so UWP apps have limitations when it wants to access system resources. This is a common cause of sql connecting problems.

    Use the checknetisolation.exe tool.

    To enable loopback use this command:

    c:\>checknetisolation loopbackexempt -a -n=<package family name>  
    

    To disable loopback use this command:

    c:\>checknetisolation loopbackexempt -d -n=<package family name>  
    

    Please check this document- Enabling loopback for a UWP application for more inforamtion.

    Thank you.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


3 additional answers

Sort by: Most helpful
  1. Ludwig Nel 21 Reputation points
    2022-06-18T08:53:49.483+00:00

    The code that is validating/testing the connection string is as follows

    public async Task ExecuteAsync(string connectionString)  
            {  
                try  
                {  
                    using (var connection = new SqlConnection(connectionString))  
                    {  
                        await connection.OpenAsync();  
                        Message = $"Database connection succeeded.";  
                        Result = Result.Ok("Database connection succeeded");  
                    }  
                }  
                catch (Exception ex)  
                {  
                    Result = Result.Error("Error creating database. See details in Activity Log");  
                    Message = $"Error validating connection: {ex.Message}";  
                }  
                PrimaryButtonText = "Ok";  
                SecondaryButtonText = null;  
            }  
    

    I don't think the problem is with the code.
    It has to be something related to UWP and permissions or something like that.

    I installed Visual Studio on the client machine and ran the project.
    When running(debugging) it from Visual Studio it runs and connects to the db with no problem.

    But creating the MSIX package and installing that version then it won't connect.

    There is also a problem accessing the install folder.
    Things crash with this line of code

     var installedLocation = Windows.ApplicationModel.Package.Current.InstalledLocation;  
                var letterHeadImage = await installedLocation.GetFileAsync("\\Assets\\ReportHeader.jpeg");  
    

    This works on my dev machine but not on the client pc

    0 comments No comments

  2. Ken Tucker 5,846 Reputation points
    2022-06-18T17:32:28.013+00:00

    I agree it is a permissions issue that is preventing access to the SQL server. Creating a web service to get and save data to a SQL server is a more secure option.

    0 comments No comments

  3. Erland Sommarskog 101.4K Reputation points MVP
    2022-06-18T20:52:18.453+00:00

    I don't think the problem is with the code.

    It has to be something related to UWP and permissions or something like that.

    I don't think so. The issue is rather with the connection string. Please share it. Although, if I am to make a guess, it has (localdb) in it. (localdb) is fine when you test in your own sandbox, but not when you deploy to a server. Keep in mind that (localdb) runs under your credentials, and it is not visible to other processes.