Unable to locate System.Data.SqlClient reference
Asked Answered
A

7

26

I have a fresh Visual Studio 2017 Professional install. I'm building a quick POC Console application using .NET 4.7.1, and I'm unable to find the reference for System.Data.SqlClient.

I have scoured my system, and located 4 versions of System.Data.SqlClient.dll, but none are correct and won't compile. I have also attempted to use System.Data, but no reference to SqlClient is located within. I have manually added the dll/reference for System.Data, but also did not resolve the reference issue.

My application is really simple at the moment, and it will NOT compile due to this missing reference.

What steps do I need to do to get this resolved?

using System;
using System.Data;
using System.Data.SqlClient;

namespace ConsoleApp1
{
    class Database
    {
        public void Start()
        {

            string connString = @"server=(local);initial     catalog=MyDatabase;Integrated Security=SSPI;";
            using (SqlConnection conn = new SqlConnection(connString))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand("SELECT TOP 10 ID, Name FROM TableA", conn))
                {
                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        while(reader.Read())
                        {
                            Console.WriteLine("ID: [{0}], Name: [{1}]", reader.GetValue(0), reader.GetValue(1));
                        }
                    }
                }
            }
        }
    }
}
Antipas answered 28/2, 2018 at 17:17 Comment(5)
When you said you can't find the reference, are you saying you missing System.Data when you go into Add Reference -> Assemblies? If you are not missing System.Data; then, is there a checked checkbox to the left of System.Data?Untrimmed
I would from menu : Project : Add Reference : Net : System.Data. Sometimes the references are not automatically added.Beamon
Correct. Under assemblies, I have System and System.Data. These are from the C:\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.netcore.app\2.0.0\ref\netcoreapp2.0 directory. However, SqlClient is not in this assembly. Both references are checked.Antipas
@AllanL you never said you were using .NET Core, System.Data is a nuget packages nowGunfire
Thanks - I had no idea they pulled this functionality out of the new version of VS. Thanks - I have it installed now.Antipas
P
44

dotnet add package System.Data.SqlClient

Psychoactive answered 6/3, 2019 at 20:52 Comment(2)
Another way would be with NuGet: install-package System.Data.SqlClientGratulation
Thanks, couldn't remember if I had the name right and for some reason searching for it in the GUI didn't return it as a hit.Levitus
A
12

Microsoft removed System.Data.SqlClient from newer versions of Visual Studio and .NET Core. The fix is to install the NuGet package System.Data.SqlClient. In other words, you might not find the DLL on your system and need to add it using NuGet.

In the Solution Explorer, right-click References and choose "Manage NuGet Packages...". Search for "System.Data.SqlClient". Choose it when found, and Install.

NuGet Package Manager

Additive answered 28/11, 2021 at 18:39 Comment(0)
I
6

You just have to add reference option in solution explorer and after that, if it is not working you need to change class library use ".net framework" apart from ".net standard"

Ibbetson answered 15/8, 2018 at 14:53 Comment(2)
This worked, as per ParagJain's answer above and @Untrimmed 's comment under the question. Right click your project, go to Add ---> Reference. Under Assemblies, look for System.Data and check the checkbox. In my case, I was not missing System.Data (was able to call System.Data in using directives but this box was still unchecked, thus not allowing me to using System.Data.SqlClient;.Marasmus
The answer provided by @Jorge Candeias works for a .NET Standard librarySidelight
V
1

In my case, I had a subtle problem different from other answers. The reference was added but it had a yellow triangle meaning I had a problem with the reference.

I had to remove it from the references dropdown

Assembly references

and reinstall the package with

Update-Package System.Data.SqlClient -Reinstall

Then it added the correct reference back.

I was pulling my hair apart for two days because I knew I had the reference, but never noticed it had the yellow triangle.

I thought it was only a mismatch version problem, but using binding redirects on web.config were of no avail.

It was then when I realized that System.Data.SqlClient.dll was not being copied into my bin folder. That was due to the wrong assembly reference. I don't know how this reference came to be corrupted though.

Volar answered 19/10, 2019 at 19:6 Comment(0)
G
0

Please check your version of SQL Server on your machine, you might have more than one version of sql server express installed on your machine. Please try to connect (localdb)/MSSQLLOCALDB from your management studio and if you are able to open try updating the connection in your program and try again.

Grisgris answered 6/3, 2019 at 20:47 Comment(0)
D
0

I got this very same issue using visual studio 2022.

from solution explorer, right click and go to manage nuGet Packages

enter image description here

very important detail, make sure you have ALL on Package source:

enter image description here

go to browse: type data.sql then you find the microsoft.data.sqlclient install it (mine on the picture is already installed so nevermind this)

enter image description here

enter image description here

enter image description here

Now you can use Microsoft.Data.SQLClient instead of System.Data.SqlClient.

enter image description here

I probably just added more details and pictures, but I believe this is a valid answer as it took me a long time to figure this out, and here it is all ready.

Detour answered 22/9, 2023 at 11:22 Comment(0)
S
0

Microsoft fixed the data source wizard.

In Visual Studio 2022 community 17.9 the SQLClient package from Microsoft.Data was just missing and NOT installable. And you could not type "using Microsoft.Data.SqlClient" because of that.

Yesterday they published the update to 17.10.1 and suddenly the data conneciton wizard can download the missing thing and finally configure something else than ODBC (which is lamer than snails). I assume that this also fixes the other stuff, e.g. no nlonger Nuget Packages needed to have the ms.data.sqlclient thing up and working.

Scriptwriter answered 4/6 at 16:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.