UWP C#SQLConnection

问题描述 投票:0回答:1

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

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

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包或其他东西,因为其余的看起来还不错。尝试连接时会暂停,然后返回错误。

任何帮助将不胜感激

c# sql-server uwp connection sql-server-express
1个回答
0
投票

确保启用以下capabilities

  • privateNetworkClientServer
  • 企业身份验证
  • internetClient
  • internetClientServer
© www.soinside.com 2019 - 2024. All rights reserved.