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.
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.
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.
- Right Click on your solution from the solution panel in VS.
- From the context menu, select the Manage NuGet Packages for solutions...
- In the NuGet Package Manager window, select the Browse Tab.
- In the search box type in System.Data.SqlClient and press enter.
- Look for the package System.Data.SqlClient by Microsoft and select it.
- 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.
- Then press the install button. Wait for the installation to complete. NuGet will install the selected package and all it’s dependencies.
- Now go back to your solution or project and rebuild. You will not get the error again.
- Introducing the new Microsoft.Data.SqlClient at Microsoft Dev Blogs.