When you create a new project in .NET, and you try to use a class from ADO.NET, such as SqlConnection or SqlDataReader, you’ll get missing reference errors.
For example, your code might look something like this, and it’ll have red-squiggly lines under SqlConnection:
using(var sqlConnection = new SqlConnection(connectionString))
{
//execute queries
}
Code language: C# (cs)
In the past, you’d simply add a reference to System.Data.SqlClient, like this:
using System.Data.SqlClient;
Code language: C# (cs)
To your surprise, after adding the reference, you get this confusing error:
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=4.6.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a’ Consider adding a reference to that assembly.
The problem is that System.Data.SqlClient is not part of the framework anymore. It’s in its own nuget package. Microsoft probably did this to decouple the framework from other Microsoft products (in this case, SQL Server).
Solution: Get the latest System.Data.SqlClient nuget package
The solution is to get the latest System.Data.SqlClient nuget package.
To install the latest, execute the following command in the Package Manager Console window (View > Other Windows > Package Manager Console):
Install-Package System.Data.SqlClient
Code language: PowerShell (powershell)
Note: You could also use the Nuget Manager UI if you want.
At the time of this writing, running this command installed System.Data.SqlClient v4.8.2.
After installing this package, the reference errors will go away.
Comments are closed.