Reputation: 2066
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
Reputation: 11
if you have this error in .net core , you shoud install System.Data.SqlClient Nuget package
Upvotes: 1
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
Reputation: 15294
First: Ensure you're referencing System.Data
not System.Data.SqlClient
Then add
using System.Data.SqlClient;
To your namespaces
Upvotes: 6