-1

I just try to connect to my database with SqlConnection, but I saw the error...

The point is I'm using System.Data.SqlClient, but it looks like it doesn't work somehow...

private string ConectionString = "Data Source=.;Initial Catalog=Contact_DB;Integrated Security=true";

public DataTable SelectAll()
{
    string query = "Select * From MyContacts";

    SqlConnection Connection = new SqlConnection(ConnectionString);
}

Also it shows this error:

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.    MyCountacts    D:\Visual Studio Projects\C#\MyCountacts\MyCountacts\Repository\Services\MyContactsRepository.cs 28   Active

What can I do now?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
AminG82
  • 1
  • 1
  • 1. Right-click on your project in Visual Studio and select "Manage NuGet Packages". 2. In the NuGet Package Manager, search for "System.Data.SqlClient". 3. Install the package "System.Data.SqlClient" if it is not already installed. if not installed , try to install in NuGet – Nima Jul 08 '23 at 10:50

1 Answers1

-2

From your error message , it looks like you're using the System.Data.SqlClient namespace which has been deprecated in .NET Core 3.1 and later. In other words, .NET has pushed it to the curb in favor of Microsoft.Data.SqlClient. So what you're seeing is just .NET being a little finicky and saying, "Hey, I don't recognize this guy anymore."

To sort this out , you need to start using Microsoft.Data.SqlClient instead. This package should have all the functionality you're used to with System.Data.SqlClient, but it's the new and hip thing that .NET Core and .NET 5.0+ want you to use.

First thing's first , go to your package manager console and run this command to get the Microsoft.Data.SqlClient package:

Install-Package Microsoft.Data.SqlClient

After you've installed that , you need to change your using statement in your C# file from:

using System.Data.SqlClient;

to:

using Microsoft.Data.SqlClient;

Then you should be able to use SqlConnection just like before. So your connection string declaration will now look like this:

private string ConectionString = "Data Source=.;Initial Catalog=Contact_DB;Integrated Security=true";

public DataTable SelectAll()
{
    string query = "Select * From MyContacts";

    SqlConnection Connection = new SqlConnection(ConnectionString);
}
  • 1
    This answer looks like it was generated by an AI (like ChatGPT), not by an actual human being. You should be aware that [posting AI-generated output is officially **BANNED** on Stack Overflow](https://meta.stackoverflow.com/q/421831). If this answer was indeed generated by an AI, then I strongly suggest you delete it before you get yourself into even bigger trouble: **WE TAKE PLAGIARISM SERIOUSLY HERE.** Please read: [Why posting GPT and ChatGPT generated answers is not currently acceptable](https://stackoverflow.com/help/gpt-policy). – tchrist Jul 08 '23 at 18:12
  • @tchrist, there is no evidence at all to indicate use of Chat GPT, the phrases "first things first" and "sort them out" are never used by chat GPT – Son of Man Jul 09 '23 at 03:46
  • Ha, there's a wise one! so nice – nail steiger Jul 09 '23 at 04:14
  • thats what i need :))) thanks so much... – AminG82 Jul 09 '23 at 08:04
  • 4
    @VibrantWaves Oh, if that were only so. See [these answers](https://stackoverflow.com/search?q=user%3A22036390+is%3Aanswer) (most are deleted, but three still remain) for a surprising twist on what LLM's might produce with a custom prompt (along with my comments which were also mostly generated by ChatGPT with a custom prompt). – NotTheDr01ds Jul 11 '23 at 01:39