0

I am currently implementing a UWP program for an existing database. I took this article as a basis. https://learn.microsoft.com/en-us/windows/uwp/data-access/sql-server-databases

When running, I get an exception:

System.Data.SqlClient.SqlException" в System.Data.SqlClient.dll

Exception:
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: 25 - Connection string is not valid)

This seemed strange to me, since a WPF application with the same connection string works fine. Next, I decided to install a database instance on my computer and export some of the data to a local database. To my surprise, after replacing the connection string from:

Data Source=sqlserver\sqlexpress

to

Data Source=.\sqlexpress

UWP program read this from the database on my computer.

I also read this article to make sure that the remote database is configured correctly. https://www.lansweeper.com/knowledgebase/a-network-related-or-instance-specific-error-occurred/

As I said, WPF programs work with this database perfectly. What are the ways to check why the UWP doesn't want to communicate with the database?

to make it clear whether the remote server is available in principle, I apply the output of the TNC command:

tnc sqlserver -p 1433 -I detailed


ComputerName            : sqlserver
RemoteAddress           : 192.168.10.11
RemotePort              : 1433
NameResolutionResults   : 192.168.10.11
MatchingIPsecRules      :
NetworkIsolationContext : Private Network
InterfaceAlias          : Ethernet
SourceAddress           : 192.168.10.20
NetRoute (NextHop)      : 0.0.0.0
TcpTestSucceeded        : True

Connection string look like:

private string connectionString = @"Data Source=sqlserver\sqlexpress;Initial Catalog=TestCatalog;User ID=*;Password=*";

The same connection string is used in VS. VS shows the contents of the table without problems. I can also delete or add new lines in the VS editor.

I'm talking about a remote database. In VS server Explorer I have a Data Connection where I am connected to a remote database. I can safely edit remote database data in VS (add, delete and update). I see the connection string and originally used it as the connection string in UWP app. enter image description here

Денис
  • 133
  • 6

1 Answers1

0

You have already taken a look at this microsoft link here. Long story short, have you followed the proposal below?

Trouble connecting to your database? In most cases, some aspect of the SQL Server configuration needs to be changed. If you're able to connect to your database from another type of desktop application such as a Windows Forms or WPF application, ensure that you've enabled TCP/IP for SQL Server. You can do that in the Computer Management console.

Check to see if you have followed the rest of the solution properly. You might need to enable the tcp port from the window firewall.

Go to firewall -> Inbound Rules -> Add TCP:1433 in the rule.

Also since the error is about the connection string, try to use only the server name/ ip/ or hostname, without the server instance.

You can also add the port number and see if this works for you. When you add it, sql server ignores the instance name (as different instances require different ports to work, so it can work as a unique identifier).

Edit: How to create a valid connection string:

Look at this answer here: How to get the connection String from a database

Athanasios Kataras
  • 25,191
  • 4
  • 32
  • 61
  • I indicated this link at the beginning. As I said, this recommendation works when the database is installed on my computer. – Денис Sep 24 '19 at 07:37
  • and I have this rule. Does not work even if the firewall is turned off – Денис Sep 24 '19 at 07:38
  • Have you tried to use the ip instead of the server name in the connection string? – Athanasios Kataras Sep 24 '19 at 07:41
  • Can you also please add your connection string without any sensitive information, but keeping the instance name for example? Did you also check the connection string generated by visual studio in the server explorer? This should definetely work. – Athanasios Kataras Sep 24 '19 at 08:21
  • added below in topic – Денис Sep 24 '19 at 08:28
  • 2
    @Любитель What about try to use Visual Studio Server explorer to see if you can browser your remote sql server? Then you can use the generated connection string to test.(it is located under your server explorer) In that way, you may be able to understand why you cannot connect to it by using your app. – Barry Wang Sep 24 '19 at 09:23
  • @Любитель Do you have access to the remote database? Check Event Log on your local machine and remote to see if there is any extra information there. – Athanasios Kataras Sep 24 '19 at 09:47
  • @BarryWang in Server Explorer i can connect to database using sql server authentication. But if i try add new server and specify the same credentials, I get an error about an invalid name or password – Денис Sep 24 '19 at 10:29
  • @Любитель By saying "I can connect to db using sql server auth", do you mean you can connect to your current remote database? If you already can connect to remote db, you can fetch the connection string from your sql explorer right now. If you still cannot connect to your remote db, then you should check the settings like Athanasios mentioned in his answer first. The key problem here is the configuration of your database. But we have no idea how you set it. – Barry Wang Sep 26 '19 at 01:36