5

Here is the code that I'm looking at -

using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;

namespace CSharpPracticeProject
{
    class Testing
    {

        public static void main()
        {
            SqlConnection myConnection = null;
            int x = 25;
            Console.WriteLine(x/4);
        }
    }

}

Error message - 'The type or namespace 'SqlConnection' could not be found.

It seems like other posts seem to solve the issue with the import, so Im just not sure what the issue is. This is Microsoft Visual Studio 2017.

5
  • 3
    Is this .NET Framework, Standard, or Core? Can you clean and rebuild? Commented May 8, 2018 at 13:45
  • Do you have the "Target framework" (project properties) set to .NET Framework X Client Profile ? Commented May 8, 2018 at 13:47
  • If you check your References do you have System.Data ? Commented May 8, 2018 at 13:48
  • By references do you mean the using keyword? This is Microsoft Visual Studio 2017, and adding using System.Data doesnt make it be recognized. Even if I enter the entire pathname when declaring the variable it isnt recognized. Commented May 8, 2018 at 13:53
  • @user "References" are links to other libraries. In .NET Framework projects this appears under "References" in the Solution Explorer. In .NET Core projects, it appears under "Dependencies" in the Solution Explorer. Commented May 8, 2018 at 13:55

1 Answer 1

20

I'm going to assume that you're using .NET Core, since it should work out the box with the code you have if you're using .NET Framework.

In .NET Core, you need to add a reference to System.Data.SqlClient via NuGet:

  1. Select Tools | NuGet Package Manager | Manage NuGet Packages for Solution
  2. Click "Browse"
  3. Type System.Data.SqlClient and hit return
  4. Select System.Data.SqlClient by Microsoft
  5. On the panel on the right, check your project.
  6. Click Install
  7. Click OK to the next box that comes up.

Your code should now work.


If you're using .NET Framework, which seems unlikely given the problem you're facing, you can check if you have System.Data referenced by right-clicking "References" in the Solution Explorer, selecting "Assemblies" and ensuring that "System.Data" is checked.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes it is .NET Core, and I just did all of that and it worked. Thank you, I would have never thought of that on my own! Now I'll have to look at the differences between the two.
I'm glad I could help :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.