Forum Discussion
Microsoft SQL: A connection was successfully established with the server, but then an error occurred
- 4 years ago
HI LucianoS ,
If you're getting this error after January 2022, possibly after migrating from
System.Data.SqlClienttoMicrosoft.Data.SqlClientor just updatingMicrosoft.Data.SqlClientto version 4.0.0 or later, it's because MS has introduced a breaking change::Breaking changes in 4.0
Changed
Encryptconnection string property to betrueby default.The default value of the
Encryptconnection setting has been changed fromfalsetotrue. With the growing use of cloud databases and the need to ensure those connections are secure, it's time for this backwards-compatibility-breaking change.Ensure connections fail when encryption is required
In scenarios where client encryption libraries were disabled or unavailable, it was possible for unencrypted connections to be made when
Encryptwas set totrueor the server required encryption.Rick Strahl has posted a detailed explanation on his blog
The change was made in this SqlClient pull-request in August 2021, where there is additional discussion about the change.
The quick-fix is to add
Encrypt=Falseto your connection-strings.Similar question refer:
Did I answer your question? Mark my post as a solution!
Best RegardsLucien
Yes, the change of encryption policy was very helpful.
However, there were other hickups when converting a project to net standard 2.0 and then using it in a winforms 4.8 project. I included my reply in another forum below in case it is of help to anyone.
---
I had the same issue in a net 4.8 WinForms project, which fixed by adding the latest System.Buffers nuget.
-- History
I converted a set of libraries used in common for winforms and asp.net from .net 4.8 to standard 2.0, which required replacing System.Data.SqlClient with Microsoft.Data.SqlClient.
Winforms however is still on 4.8. It would run fine locally, but the installed would not. After opening a connection, touching anything on a property, such as the connection property, connection state, etc., would throw an instance of object not referenced... exception.
I added the above idea (modified the path) to include the SNI artificats in the build for installer, but this was of no help.
So i manually copied over all Microsoft.*.dll assemblies from my local bin over to the installed version on local machine. Then it gave a Systems.buffer assemblies version error. While the System.Buffers versions were identical on both the local bin and the installed program files (even the revision and patch parts), the file size was different (seriously Microsoft, follow best practices).
So i added a latest nuget of System.Buffers to my winforms project, created a new build on installer (to clear out my experiments) and did a new install onto local machine, and works now.
This took a lot of trial and error. I also noticed that System.Tuples.Values (yes that one), has a different file size. So the local build used in visual studio 2022 for winforms has some secret sauce that the Project deployer does not. I also noticed that the local project bin in vs 2022 was using the full version of Microsoft.Data.SqlClient (at about 2 MB), while the installer pulled over a 72kb version (from targets).