Search code examples
c#ado.net.net-6.0

How to use SqlCommand, SqlDataReader SqlConnection etc. in Core .NET 6.0


I am trying to access database using ADO.NET using below code

using System.Data;
using System.Data.SqlClient;
...........
.......
protected readonly SqlConnection conn = new SqlConnection();
protected SqlCommand cmd = new SqlCommand();
private SqlDataReader rdr;

Visual Studio is not able to detect\verify SqlConnection\SqlCommand\SqlDataReader and giving error during build

Error (active) CS0246 The type or namespace name 'SqlConnection' could not be found (are you missing a using directive or an assembly reference?) ...API
....\DAL.cs 10

How to resolve the issue ?

Finally I got the solution :-)

using Microsoft.Data.SqlClient;

and now its working fine for me Reference : https://devblogs.microsoft.com/dotnet/introducing-the-new-microsoftdatasqlclient/


Solution

  • For .NET Core, this all moved to NuGet packages in the Microsoft.Data namespace. You have to add the Microsoft.Data.SqlClient Nuget package and change System.Data to Microsoft.Data. That's usually enough. There are some other changes few pre-existing projects will notice.