簡體   English   中英

錯誤 CS0246:找不到類型或命名空間名稱“Npgsql”(您是否缺少 using 指令或程序集引用?)

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

我開始在 Unity 中使用 PostgreSQL,但我無法使用 npgsql。 我使用 NuGet 包管理器安裝了它,但我一直遇到這個錯誤。 有人能幫我嗎? 我在互聯網上搜索了很多,但我無法弄清楚發生了什么。 npgsql 在 Assembly-CSharp 和 Assembly-CSharp-Editor 參考中。

Assembly-CSharp 參考

Assembly-CSharp-Editor 參考

我得到這個代碼只是作為如何在 Unity 中使用 PostgreSQL 的一個例子。

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;

}

您的代碼應該位於命名空間內。 缺少命名空間可能會導致您擁有命名空間。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM