-1

I can't connect to the SQL server instance in my UWP app when the instance is on the server.

Here is the situation:

  1. Using SQL Server Management Studio to connect to remote SQL Server instance, everything is fine, and I can check all databases.

  2. Using a local SQL Server instance, the app connection is established and working properly.

  3. When trying to connect to the remote SQL Server instance via the UWP app, I get the following 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)

Having tried almost every solution in this fix error topic, the problem still remains the same.

What else can I do?

Edit: Here is my connection string:

public static string DefaultConnection { get; } = "Server=172.28.17.3; Database=kcsDB; User ID=QuadM; Password=**********; MultipleActiveResultSets=true";

and ssms connection window:

connection parameters

ssms

milad
  • 71
  • 7
  • 2
    Please provide the connectionstring you use (feel free to redact the password) as well as how ssms connection window looks – siggemannen May 23 '23 at 07:46
  • @siggemannen connection string is being used by IDbConnection and Dapper – milad May 23 '23 at 09:33
  • Is the UWP app on a different computer (or mobile device) than the one on which you use SSMS? 172.28.17.3 is in a private non-routable network block (the Class B range of 172.16. 0.0 to 172.31. 255.255) so may not be accessible to your non-SSMS device. – AlwaysLearning May 23 '23 at 09:50
  • @AlwaysLearning The UWP app is on the same device as SSMS. Yes, the app is on a private network, maybe it is the reason why the UWP app is not able to connect with SQL. In this case, what can I do to make accessible for my UWP app? – milad May 23 '23 at 10:22
  • Sounds like a firewall issue, but you shouldn't connect using the IP address anyway, use a DNS name – Charlieface May 23 '23 at 10:42
  • @Charlieface I have turned the firewall off but still the same. If use the server name instead of the IP address, the error changes to: "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: 35 - An internal exception was caught)" – milad May 23 '23 at 10:59
  • I think you need to provide way more details: Server: SQL Server version + OS Version + Patch versions applied. Client: OS Version, .NET Version etc. You need to list out what you have tried. In mean time check this (if you have not already seen this): https://learn.microsoft.com/en-us/sql/connect/ado-net/sqlclient-troubleshooting-guide?view=sql-server-ver16#hostname-resolution-errors – Alex May 23 '23 at 12:57
  • 1
    Refer to the official document [Use a SQL Server database in a UWP app](https://learn.microsoft.com/en-us/windows/uwp/data-access/sql-server-databases), can it be connected normally? – Junjie Zhu - MSFT May 24 '23 at 02:47
  • 1
    @JunjieZhu-MSFT No it wasn't! Basically, the link you have provided solved my problem. Because I had moved my solution folder to a new PC, I was forgotten to enable some capabilities such as Prive networks(client & Server) and etc.. in Package .appxmanifest. After enabling these options the connection to the SQL server was established. Thank you! – milad May 24 '23 at 10:44
  • Great to hear that, I'll summarize the solution below. – Junjie Zhu - MSFT May 25 '23 at 02:18

1 Answers1

1

This issue is because the App lacks capabilities, and the App capabilities related to SQL server need to be added to Package.appxmanifest.

Open the Package.appxmanifest file of your UWP project in the manifest designer.

In the Capabilities tab, select the Enterprise Authentication checkbox if you are using Windows Authentication for authenticating your SQL Server.

You will also need to select Internet (Client & Server), Internet (Client), and Private Networks (Client & Server), regardless of whether or not you're using Windows Authentication.

Junjie Zhu - MSFT
  • 2,086
  • 1
  • 2
  • 6