I created two sample C# Console apps, one running on .NET Framework 4.6.1 and the other running .NET Core 3.1. BOTH apps are identical with the following code for Program.cs:
using System;
using Microsoft.Data.SqlClient;
namespace DBConnectTest
{
class Program
{
static void Main(string[] args)
{
string connStr;
Console.WriteLine("Type Connection String: ");
connStr = Console.ReadLine();
using (SqlConnection connection = new SqlConnection(connStr))
{
SqlCommand sqlCommand = new SqlCommand("SELECT 'Hello' UNION SELECT 'World';", connection);
sqlCommand.Connection.Open();
sqlCommand.ExecuteNonQuery();
}
Console.WriteLine("Succeeded! Done.");
Console.Read();
}
}
}
NOTE 1: I am using nuget package Microsoft.Data.SqlClient on both projects for consistency.
For input, I would copy/paste a Connection String. Sample:
Data Source=UATSOMEENV01;Initial Catalog=SomeDB_Name;Persist Security Info=True;Integrated Security=SSPI;App=SampleApp
Note 2: The SQL Server is NOT local to the server running the Apps.
Here's where it gets interesting. I run the .NET Framework 4.6.1 App executable and it connects to the DB just fine. However, when I run the .NET Core 3.1 App executable, it fails to open a connection to the DB. Here's the error message:
I have read many other questions on the internet regarding issues with connecting to the DB, but it always seems to be some issue with getting the Connection String just right. In this case, I know it's right because it works on 4.6.1 (unless .NET Core expects it differently).
Other note-worthy items:
- As an alternative, I have generated a Connection String from a .udl file that connects successfully. However, that also does not work when running the .NET Core 3.1 executable.
- DB Server referenced in Connection String is an MSSQL server on an AON cluster.
- Using Sql Server Authentication (sa account) instead of Windows Authentication on Connection String outputs the same behavior.
- Running the apps from the local DB server works on BOTH apps. ie: Using the following Connection String works when the executables are ran locally from the DB server:
Data Source=localhost;Initial Catalog=SomeDB_Name;Persist Security Info=True;Integrated Security=SSPI;App=SampleApp
Any help with this would be greatly appreciated. Thanks in advance!