简体   繁体   中英

Error while connecting UWP with remote SQL Server

I want to connect UWP to SQL Server Database. I used below code to do that.

private LogIn GetDetails(string UserName)
{
    SqlConnectionStringBuilder connectionString = new SqlConnectionStringBuilder();
    connectionString.DataSource = "IP,Port";
    connectionString.InitialCatalog = "Database Name";
    connectionString.UserID = "sa";
    connectionString.Password = "Password";
    var logIn = new LogIn();
    try
    {
        using (SqlConnection conn = new SqlConnection(connectionString.ConnectionString))
        {
            conn.Open();
            if (conn.State == System.Data.ConnectionState.Open)
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = $"SELECT * FROM Login where UserName = '{UserName}'";
                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            logIn.UserName = reader.GetString(0);
                            logIn.Password = reader.GetString(1);
                            logIn.Type = reader.GetString(2);
                        }
                    }
                }
            }
        }
        return logIn;
    }
    catch (Exception)
    {
        return null;
    }
}

This code throws below 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

So I tried to open the SQL Server database in Server Explorer of Visual Studio. It connection opened properly but If I open the Tables folder It shows

在此处输入图片说明

So I tried to open the SQL Server database in SQL Server Management Studio. In that, It works properly. Connection opened properly and also I can view the tables inside Tables folder.

在此处输入图片说明

Don't have any idea about the place of error. Maybe in Code or Server Explorer of Visual Studio or SQL Server configuration of the server computer.

Solution tried

  • Creating a new SQL Server Login account and log in through new the new account
  • Enabled Mixed Mode using below command

     ALTER LOGIN [sa] WITH PASSWORD='newpassword', CHECK_POLICY=OFF GO ALTER LOGIN [sa] ENABLE GO 

You need to ensure that the required networking capabilities are declared in your appx manifest. Depending on your networking setup/configuration these are the ones to check for:

<Capability Name="internetClient" />
<Capability Name="internetClientServer" />
<Capability Name="privateNetworkClientServer" />
<uap:Capability Name="enterpriseAuthentication" />

You need to enable Mixed Mode and 'sa' user for logon.

ALTER LOGIN [sa] WITH PASSWORD='newpassword', CHECK_POLICY=OFF
GO
ALTER LOGIN [sa] ENABLE
GO

Taken from here .

Set below capabilities in Package.appxmanifest

  • Internet (Client & Server)
  • Private Network (Client & Server)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM