user14544642
user14544642

Reputation:

error CS0246: The type or namespace name 'Npgsql' could not be found (are you missing a using directive or an assembly reference?)

I started using PostgreSQL with Unity, but I can't use the npgsql. I installed it using the NuGet Package Manager but I keep having this error. Can someone help me? I searched a lot in the internet but I can't figure out what's happening. The npgsql are in both Assembly-CSharp and Assembly-CSharp-Editor references.

Assembly-CSharp references

Assembly-CSharp-Editor references

I get this code just as an example of how using PostgreSQL with Unity.

using UnityEngine;
using Npgsql;

public class BancoDeDados : MonoBehaviour
{

    // Start is called before the first frame update
    void Start()
    {
          string connectionString =
          "Server=Projeto E-Battle;" +
          "Database=e-battle;" +
          "User ID=postgres;" +
          "Password=senha;";
       // IDbConnection dbcon; ## CHANGE THIS TO
        NpgsqlConnection dbcon;
 
       dbcon = new NpgsqlConnection(connectionString);
       dbcon.Open();
       //IDbCommand dbcmd = dbcon.CreateCommand();## CHANGE THIS TO
        NpgsqlCommand dbcmd = dbcon.CreateCommand();
       // requires a table to be created named employee
       // with columns firstname and lastname
       // such as,
       //        CREATE TABLE employee (
       //           firstname varchar(32),
       //           lastname varchar(32));
       string sql =
           "SELECT id_tema, nome " +
           "FROM temas";
       dbcmd.CommandText = sql;
       //IDataReader reader = dbcmd.ExecuteReader(); ## CHANGE THIS TO

      NpgsqlDataReader reader = dbcmd.ExecuteReader();
      while(reader.Read()) {
            string id_tema = (string) reader["id_tema"];
            string nome = (string) reader["nome"];
            System.Console.WriteLine("Name: " +
                 id_tema + " " + nome);
       }
       // clean up
       reader.Close();
       reader = null;
       dbcmd.Dispose();
       dbcmd = null;
       dbcon.Close();
       dbcon = null;

}

Upvotes: 1

Views: 2864

Answers (1)

Andi Thomaj
Andi Thomaj

Reputation: 85

Your code should be inside a namespace. The absence of a namespace might cause the one you have.

Upvotes: 1

Related Questions