简体   繁体   中英

UWP C# SQLConnection

I am writing a database app in UWP with SQL Server Express. I use this SQL database for other apps that I've written in C#.NET, but I'm trying to learn UWP for it's updated interface and MVVM/MVC style.

I have the correct includes for accessing the database:

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

I have my connection string (in App.xaml.cs), same as on the other machine (u/p obviously masked):

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

I have my database open function, which returns a dataset:

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;
}

It works fine in my .NET program just like this.

Comparing it to https://docs.microsoft.com/en-us/windows/uwp/data-access/sql-server-databases is close enough to be the same thing, except I'm returning to a dataset instead of a collection of a class. (I'll get there, that isn't the problem).

As I said, this works just fine in .NET, it's a straight across port. The problem occurs on the conn.Open(); It errors with this:

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)

If, instead of the server name, I use the 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)

The only thing that has changed with it is the IP. Both these work with .NET. it matches the connection string suggested by 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";

I'm thinking that I must be missing a nuget package or something, because the rest seems fine. It pauses when it's trying to connect, then returns the error.

Any help would be appreciated

Make sure the following capabilities are enabled:

  • privateNetworkClientServer
  • enterpriseAuthentication
  • internetClient
  • internetClientServer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM