Redlik
Redlik

Reputation: 549

What is wrong with this code. Why can't I use SqlConnection?

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

Answers (12)

Ali Yousefi
Ali Yousefi

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

Chad Hedgcock
Chad Hedgcock

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

HarshSharma
HarshSharma

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.

enter image description here

Upvotes: 0

Akang Toshi
Akang Toshi

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

Aminur Rahman
Aminur Rahman

Reputation: 410

NuGet Package

Install-Package System.Data.SqlClient

solve my problem.

Upvotes: 0

Ian
Ian

Reputation: 4467

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

Upvotes: 14

prineth fernando
prineth fernando

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.

Click here to screenshot

Upvotes: 0

ADH - THE TECHIE GUY
ADH - THE TECHIE GUY

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.

enter image description here

Hope this is fix Your Error

Upvotes: 38

Asad Naeem
Asad Naeem

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

Indiana Lora
Indiana Lora

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

I installed:

  • SQL server compact toolbox
  • Nuget Package version updater

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

Upvotes: 0

AMouat
AMouat

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

jazb
jazb

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

Related Questions