Error: ‘SqlConnection’ could not be found in the namespace (Solved)

Problem

Recently while trying to create a simple console application in C# .NET 5.0, I got the below error while building the application. The SqlConnection, SqlCommand and SqlDataReader are not recognized even after adding the namespace System.Data.SqlClient on top of the program.

Error CS1069 The type name ‘SqlConnection’ could not be found in the namespace ‘System.Data.SqlClient’. This type has been forwarded to assembly ‘System.Data.SqlClient, Version=0.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a’ Consider adding a reference to that assembly.

SqlConnection Could Not Be Found

Reason

The reason for this error is that the namespace System.Data.SqlClient will not be referenced by the .NET Core project by default as done by .NET Framework, So, in .NET Core, you have to manually add the package to the project.

Solution

The solution is to Install and use the new System.Data.SqlClient package in NuGet to your project or solution. Follow these steps to install the package.

  1. Right Click on your solution from the solution panel in VS.
  2. From the context menu, select the Manage NuGet Packages for solutions...
  3. In the NuGet Package Manager window, select the Browse Tab.
  4. In the search box type in System.Data.SqlClient and press enter.
  5. Look for the package System.Data.SqlClient by Microsoft and select it.
  6. A small panel will open at the right side of the window with the list of projects in your solution. Select only the projects you want the SqlClient package to be installed.
  7. Then press the install button. Wait for the installation to complete. NuGet will install the selected package and all it’s dependencies.
  8. Now go back to your solution or project and rebuild. You will not get the error again.
Solution for Error: 'SqlConnection' Could Not Be Found In The Namespace

Reference


9 thoughts on “Error: ‘SqlConnection’ could not be found in the namespace (Solved)”

Leave your thoughts...

This site uses Akismet to reduce spam. Learn how your comment data is processed.