【问题标题】:UWP C# SQLConnectionUWP C# SQLConnection
【发布时间】:2020-03-25 21:15:57
【问题描述】:

我正在使用 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";

我有我的数据库打开函数,它返回一个数据集:

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 进行比较足以得出相同的结果,只是我返回的是数据集而不是类的集合。 (我会到达那里,这不是问题)。

正如我所说,这在 .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 包或其他东西,因为其余的看起来都很好。它在尝试连接时暂停,然后返回错误。

任何帮助将不胜感激

【问题讨论】:

  • 未包含功能>企业身份验证。做了,还是不行。还在寻找。
  • 验证 TCP/IP 已启用,SQL 浏览器正在运行。
  • privateNetworkClientServer 能力?
  • @NicoZhu-MSFT - 正如我在第一条评论中所述,没有。

标签: c# sql-server uwp connection sql-server-express


【解决方案1】:

确保启用以下capabilities

  • privateNetworkClientServer
  • 企业认证
  • 互联网客户端
  • internetClientServer

【讨论】:

  • 是的。就是这样。完美的。根据 MS 教程,我已经启用了 enterpriseAuth,但没有启用其他三个。
猜你喜欢
  • 2011-05-26
  • 1970-01-01
  • 1970-01-01
  • 2018-05-29
  • 2021-06-27
  • 2019-05-04
  • 2015-02-06
  • 2011-03-23
  • 1970-01-01
相关资源
最近更新 更多