user2962142
user2962142

Reputation: 2066

SqlConnection namespace not found

I'm building up a website using C#, whenever I try this

SqlConnection con = new SqlConnection ();

I get the error that SqlConnection namespace isn't found, but when I do this

System.Data.SqlClient.SqlConnection con;

it works.

Tried to add reference to System.Data.SqlClient, but couldn't find it the references lists even though System.Data is added assembly in the web.config file.

Upvotes: 7

Views: 8316

Answers (3)

Fatemeh Mirarab
Fatemeh Mirarab

Reputation: 11

if you have this error in .net core , you shoud install System.Data.SqlClient Nuget package

Upvotes: 1

yaakov
yaakov

Reputation: 5851

As the MSDN Documentation states, SqlConnection exists in the System.Data.SqlClient namespace, inside the System.Data assembly.

A class's namespace and assembly name do not always match.

Since System.Data.SqlClient.SqlConnection works for you, you must already have a reference to System.Data. To fix the error, add a using statement for System.Data.SqlClient to the top of your file.

If you're using Visual Studio, you can right-click on SqlConnection and let Visual Studio find the correct namespace and add a using for you. This works for any class where you already have a reference to the assembly that contains it.

Upvotes: 1

Aydin
Aydin

Reputation: 15294

First: Ensure you're referencing System.Data not System.Data.SqlClient

Then add

using System.Data.SqlClient;

To your namespaces

Upvotes: 6

Related Questions