UWP C# SQLConnection

UWP C# SQLConnection

我正在使用 SQL Server Express 在 UWP 中编写数据库应用程序。 我将此 SQL 数据库用于我用 C#.NET 编写的其他应用程序,但我正在尝试学习 UWP,因为它具有更新的界面和 MVVM/MVC 风格。

我有访问数据库的正确包含:

using System.Data;
using System.Data.SqlClient;

我有我的连接字符串(在 App.xaml.cs 中),与另一台机器上的相同(u/p 显然被屏蔽):

public static string connectionString = @"Data Source=SERVER-FS01\SQLEXPRESS; Initial Catalog=test_prds_jl; User ID=user; Password=password";

我有我的数据库打开功能,其中returns一个数据集:

public static DataSet retrieveDataFromSQL(string qry, string errorString)
{
    DataSet ds = new DataSet();
    try
    {
         SqlConnection conn = new SqlConnection(App.connectionString);
         Debug.WriteLine(App.connectionString);

         conn.Open();

         SqlDataAdapter da = new SqlDataAdapter(qry, conn);

         da.Fill(ds);

         conn.Close();
   }
   catch (Exception ex)
   {
         Debug.WriteLine(errorString + 
             System.Environment.NewLine + 
             System.Environment.NewLine + 
             queryString + 
             System.Environment.NewLine + 
             System.Environment.NewLine + 
             "Message from server: " + 
             ex.Message);

    }
    return ds;
}

它在我的 .NET 程序中运行良好,就像这样。

将其与 https://docs.microsoft.com/en-us/windows/uwp/data-access/sql-server-databases 进行比较,可以说是同一件事,只是我返回的是数据集而不是 class 的集合。 (我会到达那里,这不是问题)。

正如我所说,这在 .NET 中工作得很好,它是直接跨端口的。 问题出现在 conn.Open(); 上,错误是这样的:

Message from server: 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: 40 - Could not open a connection to SQL Server)

如果我使用 IP 而不是服务器名称:

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)

唯一改变的是IP。这两者都适用于 .NET。它匹配 MS 建议的连接字符串。

    // This is an example connection string for using SQL Server Authentication.
    // private string connectionString =
    //     @"Data Source=YourServerName\YourInstanceName;Initial Catalog=DatabaseName; User Id=XXXXX; Password=XXXXX";

我想我一定是少了一个 nuget 包之类的,因为其余的似乎都很好。它在尝试连接时暂停,然后 returns 出现错误。

如有任何帮助,我们将不胜感激

确保以下 capabilities 已启用:

  • privateNetworkClientServer
  • 企业身份验证
  • 互联网客户端
  • 互联网客户端服务器