简体   繁体   中英

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

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?

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;

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