Reputation: 549
I am 100% newbie to SQl and wanted to make a ConsoleApp with the use of database. I read some about it and tried. When I needed to make SqlConnection, my VS 2019 Preview showed me this
Severity Code Description Project File Line Suppression State 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. ConsoleApp1 C:\Users\User\Desktop\Bald Code\ConsoleApp1\ConsoleApp1\Program.cs 12 Active
i don't get why it doesn't work
Here's my code
using System;
using System.Data;
using System.Data.SqlClient;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
string connectionString;
SqlConnection cnn;
}
}
}
Upvotes: 41
Views: 86098
Reputation: 2765
I encountered a problem in my .NET MAUI application targeting .NET 8.0; my Android application wouldn’t compile after I installed the System.Data.SqlClient package from NuGet. But after uninstalling it and installing the Microsoft.Data.SqlClient package instead, the problem was resolved.
Install-Package Microsoft.Data.SqlClient
Upvotes: 0
Reputation: 11785
If you just updated EntityFrameworkCore from version 2.x to 3.x and you're running into this, change your using
statement to Microsoft.Data.SqlClient instead of System.Data.SqlClient.
If you're using EntityFrameworkCore.SqlServer it already has that as a dependency, so you shouldn't need to install it explicitly.
This Microsoft blog explains the change.
The Microsoft.Data.SqlClient package, ... , will be the flagship data access driver for SQL Server going forward.
and
We have no intention of dropping support for System.Data.SqlClient any time soon (written in 2019). It will remain as-is and we will fix important bugs and security issues as they arise. ...
However, Microsoft.Data.SqlClient will be the only place we will be implementing new features going forward.
Upvotes: 67
Reputation: 660
If none of the previous solutions has worked for you then try changing the version of System.Data.SqlClient
in the project file to 0.0.0
or 4.1.0
because this is what the error is saying to use this version. After you change the version compile the code and once compiled then you can change the version of the library to the original one.
Note: The current version of the library I had was 4.8.5
. Also, I tried all of the above solutions but none worked for me except for this one.
Upvotes: 0
Reputation: 191
For me System.Data.SqlClient was already installed. What i had to do was, I went to the Nuget package manager and downgrade the version to 4.8.2 from 4.8.3 and it worked.
Upvotes: 0
Reputation: 410
NuGet Package
Install-Package System.Data.SqlClient
solve my problem.
Upvotes: 0
Reputation: 4467
The specific way to fix this from within VS Code is to
Open a terminal by going to Terminal -> New Terminal
Run dotnet add package System.Data.SqlClient
Run dotnet restore
That last command might not be necessary, but doing this made it work for me. It seems that console app templates don't come prepared for a reference to SqlClient.
Update
I think moving forward projects should use Microsoft.Data.SqlClient and not System.Data.SqlClient: https://devblogs.microsoft.com/dotnet/introducing-the-new-microsoftdatasqlclient/.
Upvotes: 14
Reputation: 13
It's pretty simple
Go to solution explorer > right click & click manage NuGet packages > and install System.data.SqlClient 4.5.1. or later.
Upvotes: 0
Reputation: 4393
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. You have missed the database choice when You install 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.
Hope this is fix Your Error
Upvotes: 38
Reputation: 636
[!["Manage Nuget Package for Solution"]
it fixed my issue. [1]: https://i.sstatic.net/hpk2e.png
I just installed nuget package from the "Manage Nuget Package for Solution". Previously I had used through command but that did not work so I used the UI to install this package.
Upvotes: 0
Reputation: 1
I had that error today and what's happening its that VS CODE its no recognizing System.Data.SqlClient;, that's why you can't call the variable...
How did I fix it??
Close visual studio/Open it again and your program should be rocking and popping!!
Upvotes: 0
Reputation: 755
As @Mmm mentioned in the comments if you are using .NET Core and have already installed the System.Data.SqlClient package, closing and reopening the project resolved the issue for me too.
Upvotes: 3
Reputation: 5801
Assuming that you're using .NET Core - just add NuGet package: System.Data.SqlClient
Your .csproj might look similar to:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Data.SqlClient" Version="4.5.1" />
</ItemGroup>
</Project>
Upvotes: 26