简体   繁体   English

我无法从 UWA 应用程序连接到 SQL 服务器,但我可以使用 WinForms 应用程序

[英]I can't connect to a SQL Server from a UWA app, but I can with a WinForms app

I built a small Universal Windows app (C#) but I can't connect to my SQL server.我构建了一个小型通用 Windows 应用程序(C#),但我无法连接到我的 SQL 服务器。 The exception reads:异常内容如下:

A network-related or instance-specific error occurred while establishing a connection to SQL Server.与 SQL 服务器建立连接时发生与网络相关或特定于实例的错误。 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.验证实例名称是否正确,并且 SQL 服务器配置为允许远程连接。 (provider: TCP Provider, error: 40 - Could not open a connection to SQL Server (提供者:TCP 提供者,错误:40 - 无法打开与 SQL 服务器的连接

The small section of code is simply:一小段代码很简单:

try
{
    using (SqlConnection conn = new SqlConnection(GetConnectionString()))
    {
        await conn.OpenAsync();

        if (conn.State == System.Data.ConnectionState.Open)
        {
            using (var cmd = conn.CreateCommand())
            {
                cmd.CommandText = getAllRunSheetsQuery;

                using (var reader = await cmd.ExecuteReaderAsync())
                {
                    while (await reader.ReadAsync())
                    {
                        var runNumber = reader.GetInt32(0);
                    }
                }
            }
        }
    }
}
catch (Exception eSql)
{
    System.Diagnostics.Debug.WriteLine($"Exception: {eSql.Message} {eSql.InnerException?.Message}");
    throw;
}

The connection string is:连接字符串是:

Persist Security Info=False;Application Name=XBAR_Digital_Form;Connect Timeout=2;server=wcsql.wc.com;database=ppd_general_use;User ID=*******;Password=********; Persist Security Info=False;Application Name=XBAR_Digital_Form;Connect Timeout=2;server=wcsql.wc.com;database=ppd_general_use;User ID=*******;Password=********;

I've also tried a variety of other connections string options to connect to a SQL Server version 2012.我还尝试了各种其他连接字符串选项来连接到 SQL 服务器版本 2012。

The exception gets thrown at "await conn.OpenAsync()" after the connection timeout.连接超时后,在“await conn.OpenAsync()”处引发异常。 I've confirmed through debugging that the GetConnectionString() method is passing the string correctly.我已经通过调试确认 GetConnectionString() 方法正确地传递了字符串。

The weird part is, I can connect just fine with a standard Windows Form app I made with the exact same connection string.奇怪的是,我可以用完全相同的连接字符串制作的标准 Windows Form 应用程序连接得很好。 I'm using the same general method to connect using System.Data.Sqlclient on both apps.我使用相同的通用方法在两个应用程序上使用 System.Data.Sqlclient 进行连接。 I can connect to the server from any other app (like Excel or Microsoft SQL Server Management Studio)我可以从任何其他应用程序(如 Excel 或 Microsoft SQL Server Management Studio)连接到服务器

I found the problem.我发现了问题。

I used the Windows Template Studio to create the project.我使用 Windows Template Studio 来创建项目。 I looked at my Package.appxmanifest and went to Capabilities and found "Private Networks (Client & Server)" was not enabled.我查看了我的 Package.appxmanifest 并转到功能,发现“私有网络(客户端和服务器)”未启用。 After enabling it and rebuilding, the connection worked flawlessly.启用并重建后,连接完美无缺。


can't access to the server on network you have to allow remote connections add TCP and UDP无法访问网络上的服务器,您必须允许远程连接添加 TCP 和 UDP

  • open file explorer post this path "Control Panel\All Control Panel Items\Windows Defender Firewall "打开文件资源管理器发布此路径“控制面板\所有控制面板项目\Windows Defender 防火墙”
  • on the left side choose advanced settings在左侧选择高级设置
  • add new Inbound Rule TCP and port 1433添加新的入站规则 TCP 和端口 1433
  • add new Inbound Rule UDP and port 1434添加新的入站规则 UDP 和端口 1434

do the same for outbound对出站做同样的事情

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法从本地应用连接到SQL Server别名 - Can't connect to SQL Server alias from local app 是否有一个紧凑或快速的SQL Server版本,我可以使用我的WinForms应用程序打包并且是免费的? - Is there a compact or express SQL Server version that I can package with my WinForms app & is free? C# Winforms 使用 TcpListener 我无法从局域网上的其他设备连接到服务器 - C# Winforms using TcpListener I can't connect to server from other devices on my LAN 网站无法连接到SQL Server,但控制台应用程序可以 - Website can't connect to SQL Server, but console app can 为什么不能让WinForms应用程序注册COM互操作? - Why can't I make a WinForms app register for COM interop? 如何从 Winforms 应用程序捕获 WPF 事件 - How can I capture WPF events from a Winforms app 如何在WinForms应用程序中接受击键? - How can I accept keystrokes in a WinForms app? 无法从运行在 docker 上的 .net 核心应用连接到 SQL Server express - Can't connect to SQL Server express from .net core app running on docker 无法使用来自 UWP-App 的可信连接连接到 SQL Server - Can't connect to SQL Server using Trusted Connection from UWP-App 无法从Xamarin.Forms应用连接到SignalR服务器 - Can't connect to SignalR server from Xamarin.Forms app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM