spirrrnd
spirrrnd

Reputation: 17

Can't connect to SQL Server database from UWP

I have a UWP C# app and I need to connect to my SQL Server Express database.

When I connect to the database from tools option in Visual Studio, I can copy the connection string. Connectivity tests successfully, but when I try to connect with code I always get this error:

System.Data.SqlClient.SqlException: 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)

    string connectionString =
         @"Data Source=DESKTOP-IF672GA\SQLEXPRESS;Initial Catalog=TestDatabase;Integrated Security=True";

    SqlConnection sc = new SqlConnection(connectionString);
    sc.Open();

Upvotes: 0

Views: 706

Answers (4)

s00103898-276165-15433
s00103898-276165-15433

Reputation: 998

enable Named Pipes, and TCP/IP.

  1. Type compmgmt.msc in the CommandLine, to Open SQL Server Configuration Manager.
  2. Enable Named Pipes, and TCP/IP.
  3. restart SQL service and Server Browser.

enter image description here

enter image description here

If not work, 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. and following:

  • privateNetworkClientServer
  • enterpriseAuthentication
  • internetClient
  • internetClientServer

enter image description here

enter image description here

https://learn.microsoft.com/en-us/windows/uwp/data-access/sql-server-databases#first-set-up-your-solution

Upvotes: 0

spirrrnd
spirrrnd

Reputation: 17

Enabling TPC/IP in SQL Server configuration manager helped.

Upvotes: 1

dear_vv
dear_vv

Reputation: 2358

I can reproduce your issue when I use Windows authentication. Could you please try to use SQL Server authentication instead of Windows authentication?

As follows:

string connectionString =@"Data Source = DESKTOP-IF672GA\SQLEXPRESS;Initial Catalog = TestDatabase;User ID=xxx;Password=xxxxx";

Upvotes: 0

snj
snj

Reputation: 100

Lookup UWP sandboxing as a likely cause.

Upvotes: 0

Related Questions