'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.



Solution 1:[1]

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.

Solution 2:[2]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 yaakov
Solution 2 Fatemeh Mirarab