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/

1
  • 2
    Note: it is pretty unusual to want to store a command / reader in a field, and frankly even connections may be better scoped to a particular method; if you're not 100% sure exactly what you're doing with ADO.NET, it may also be preferable to use tools like Dapper which do all the hard work for you, so you can focus on the interesting things like what you're trying to achieve, i.e. your SQL, parameters, and data outputs - without having to worry about the glue that makes it all happen (which isn't necessarily very interesting, as code goes, and is easy to get wrong or incomplete) Commented Oct 16, 2024 at 21:40

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I mean, technically OP could also simply install System.Data.SqlClient, but since that is being phased out, yes it would be preferable if OP installed Microsoft.Data.SqlClient and migrated to it...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.