[英]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
do the same for outbound对出站做同样的事情
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.