SQLConnection not part of System.Data.SqlClient in VS 2019?

Viewed 8207

I am using Visual Studio 2019, C#, and added

Using System.Data.SqlClient;

When I use the following code:

SqlConnection cnn;

I get the error:

Severity Code Description Project File Line Suppression State Error CS1069 The type name 'SqlConnection' could not be found in the namespace 'System.Data.SqlClient'. This type has been forwarded to assembly 'System.Data.SqlClient, Version=4.6.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' Consider adding a reference to that assembly

Is the SqlConnection type no longer part of the Data SQLClient in Visual Studio 2019?

3 Answers

This all comes down to what packages you have installed; SqlClient is no longer part of System.Data.Common, so to use System.Data.SqlClient you would need a package reference to:

<PackageReference Include="System.Data.SqlClient" Version="4.8.1" />

However, it is recommended (when possible) to prefer the new

<PackageReference Include="Microsoft.Data.SqlClient" Version="2.0.0" />

instead when possible; this is the replacement, but requires a different namespace (Microsoft.Data.SqlClient instead of System.Data.SqlClient)

If you in .net core 3.x, please use NuGet to get Microsoft.Data.SqlClient. Then call "using Microsoft.Data.SqlClient;". Check Step 1 and Step 2.

you should use: using Microsoft.Data.SqlClient;

not: using System.Data.SqlClient;

Related