30

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;
        }
    }
}
Coder
  • 338
  • 2
  • 19
Redlik
  • 439
  • 1
  • 4
  • 6

11 Answers11

47

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.

Chad Hedgcock
  • 11,125
  • 3
  • 36
  • 44
29

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.

enter image description here

Hope this is fix Your Error

ADH - THE TECHIE GUY
  • 4,125
  • 3
  • 31
  • 54
26

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>
jazb
  • 5,498
  • 6
  • 37
  • 44
  • 4
    I had a similar problem despite adding the package from NuGet in VS2017. I had to close the project and re-open it for System.Data.SqlClient to be recognized. – Mmm Mar 27 '19 at 23:29
10

The specific way to fix this from within VS Code is to

  1. Open a terminal by going to Terminal -> New Terminal

  2. Run dotnet add package System.Data.SqlClient

  3. 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/.

Ian
  • 4,169
  • 3
  • 37
  • 62
2

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.

AMouat
  • 685
  • 15
  • 27
0

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??

I installed:

  • SQL server compact toolbox
  • Nuget Package version updater

Close visual studio/Open it again and your program should be rocking and popping!!

0

[!["Manage Nuget Package for Solution"]

it fixed my issue. [1]: https://i.stack.imgur.com/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.

Asad Naeem
  • 558
  • 8
  • 14
0

NuGet Package

Install-Package System.Data.SqlClient

solve my problem.

Aminur Rahman
  • 400
  • 1
  • 6
  • 14
0

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.

Akang Toshi
  • 193
  • 5
  • 17
0

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.

enter image description here

HarshSharma
  • 630
  • 3
  • 9
  • 34
-1

It's pretty simple

Go to solution explorer > right click & click manage NuGet packages > and install System.data.SqlClient 4.5.1. or later.

Click here to screenshot

  • 1
    welcome to stack overflow. You answer is the same as many of the previous ones and does not add anything new or helpful to the post. When you have enough reputation, you could upvote an existing answer instead of duplicating it which you should avoid – Guy Levy May 20 '21 at 20:19