The type name 'SqlConnection' could not be found in the namespace 'System.Data.SqlClient'

186 Views Asked by At

I'm using VIM text editor to write C# code in Linux and having this issue. I've read that using the same namespace in different files is obviously fine, and made sure spelling wasn't a problem... It also doesn't seem like it's a problem with the OmniSharp plugin.
In the terminal I tried dotnet add package System.Data.SqlClient, but it hasn't stopped from producing the warning: 'Unnecessary using directive.'

using System; using System.Data.SqlClient;

namespace myexamples {

class Drinks
{
string _connString = "Data Source=localhost;Initial Catalog=TestDB;";

public List<string> GetDrinks()
{
    List<string> drinksList = new List<string>();
    using (SqlConnection cnxn = new SqlConnection(_connString))
    {
    using (SqlCommand drinksCommand = new SqlCommand())
    {
        drinksCommand.Connection = cnxn;
        drinksCommand.CommandText = "SELECT name FROM Drinks";
        cnxn.Open();
        using (SqlDataReader drinksDataReader = drinksCommand.ExecuteReader())
        {
          while (drinksDataReader.Read() == true)
          {
             ...
          }
        }
    }
    }
}

} }

1

There are 1 best solutions below

0
On

You can use sqlconnection object instead of sqlstringbuilder and it will work fine

using(sqlconnection con=new sqlconnection)
{

enter code here}