21

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));
                        }
                    }
                }
            }
        }
    }
}
Pika Supports Ukraine
  • 3,612
  • 10
  • 26
  • 42
Allan L
  • 247
  • 1
  • 2
  • 9
  • 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`? – Tyler Feb 28 '18 at 17:23
  • I would from menu : Project : Add Reference : Net : System.Data. Sometimes the references are not automatically added. – jdweng Feb 28 '18 at 17:32
  • 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. – Allan L Feb 28 '18 at 17:35
  • @AllanL you never said you were using .NET Core, `System.Data` is a nuget packages now – maccettura Feb 28 '18 at 17:36
  • Thanks - I had no idea they pulled this functionality out of the new version of VS. Thanks - I have it installed now. – Allan L Feb 28 '18 at 17:44
  • 1
    This issue is closed. The .NET Core has removed this functionality from the base Visual Studio install. In order to use it, you must use NuGet to install the package (System.Data.SqlClient) after which it is accessible. Thanks to those who helped me with this issue. – Allan L Feb 28 '18 at 18:11
  • @AllanL if you want to have this question marked as answered I can add an answer. If you plan on deleting this then I wont bother. – maccettura Feb 28 '18 at 19:23
  • Thank you - That would be great. – Allan L Mar 02 '18 at 16:50

6 Answers6

39

dotnet add package System.Data.SqlClient

Jorge Candeias
  • 670
  • 6
  • 12
8

New issue. 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

Allbite
  • 2,367
  • 1
  • 24
  • 22
5

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"

Parag Jain
  • 612
  • 2
  • 14
  • 31
  • 1
    This worked, as per ParagJain's answer above and @Tyler '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;`. – Kyle Vassella Oct 22 '18 at 23:07
  • 1
    The answer provided by @Jorge Candeias works for a .NET Standard library – Austin Born Jun 03 '19 at 15:01
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.

R. Schreurs
  • 8,587
  • 5
  • 43
  • 62
Reuel Ribeiro
  • 1,419
  • 14
  • 23
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.

Bharath
  • 115
  • 1
  • 8
0

Enough to add reference "System.Data.SqlClient"; The same answer as above, simply select LIB class ".net framework" while it`s needed to add lib class into the your project.