Share via


The type or namespace name 'SqlConnection' could not be found

Question

Sunday, October 22, 2017 8:48 PM

Hi,

I'm new and I recently encountered an issue with SqlConnection and SqlClient. 
After reading alot of posts where mostly the issue was the missing using statement, I'm only hoping I'll get some help here.

I'm getting the next error: 'The type or namespace name 'SqlConnection' could not be found' when trying to create a SqlConnection, even if including 'using System.Data.SqlClient' in my file.

There is no errors in the using statement but altough, I'm getting a weird (grey) info note saying 'Using directive is unnecessary'.

Am I missing something? Did I just forgot a feature to install? 

I'd really appreciate help on this,
thanks alot!

All replies (5)

Sunday, October 22, 2017 9:30 PM

**Am I missing something? Did I just forgot a feature to install? **

Most likely the  System.Data.SqlClient.DLL in not in the Bin folder, and you have not set reference to the DLL in the project.


Monday, October 23, 2017 8:31 AM | 2 votes

Hello EldarJa,

>>Am I missing something? Did I just forgot a feature to install?

Yes,you have missed the database choice when you instal visual studio. And now you just manually add references named "System.Data.SqlClient".

1.right click you project name and choose nuget package options.

2.search "System.Data.SqlClient" and install it.

And if you have any issues please feel free to contact me.

Sincerely,

Neil Hu

MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact [email protected].


Monday, October 23, 2017 2:13 PM | 2 votes

You shouldn't need a NuGet package unless you're compiling a .NET core app. System.Data.SqlClient is part of the System.Data assembly which is part of the core framework. It is normally automatically added to any new project but can be removed. Adding it back should resolve the issue for non-.NET core apps.

Posting the code would be useful as the "don't need this namespace" would be indicative of something else going wrong. You need that namespace if you're using SqlConnection. My guess is that there is a typo in the code. You should be able to remove the using statement, right click SqlConnection and then the quick actions should give you the option to add the using statement. If you don't see that option then either System.Data is not being referenced or there is something else going on.

Michael Taylor http://www.michaeltaylorp3.net


Friday, October 5, 2018 2:11 AM

I keep getting errors when I try installing System.Data.SqlClient from NuGet. This is the output from Package Manager: 

Attempting to gather dependency information for package 'System.Data.SqlClient.4.5.1' with respect to project 'Assembly-CSharp', targeting '.NETFramework,Version=v3.5'Gathering dependency information took 382.04 msAttempting to resolve dependencies for package 'System.Data.SqlClient.4.5.1' with DependencyBehavior 'Lowest'Resolving dependency information took 0 msResolving actions to install package 'System.Data.SqlClient.4.5.1'Resolved actions to install package 'System.Data.SqlClient.4.5.1'Retrieving package 'System.Data.SqlClient 4.5.1' from 'nuget.org'.Install failed. Rolling back...Package 'System.Data.SqlClient.4.5.1' does not exist in project 'Assembly-CSharp'Package 'System.Data.SqlClient.4.5.1' does not exist in folder 'C:\Users\kids only\Desktop\Unity Projects\Cool YT RPG\packages'Executing nuget actions took 5.54 secCould not install package 'System.Data.SqlClient 4.5.1'. You are trying to install this package into a project that targets '.NETFramework,Version=v3.5', but the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author.Time Elapsed: 00:00:06.1397891========== Finished ==========

Friday, October 5, 2018 3:34 AM

Please post your question in a new thread. This thread was related to .NET Framework and you're referring to the NuGet package for .NET Core.

Michael Taylor http://www.michaeltaylorp3.net