【问题标题】:error CS0246: The type or namespace name 'Npgsql' could not be found (are you missing a using directive or an assembly reference?)错误 CS0246:找不到类型或命名空间名称“Npgsql”(您是否缺少 using 指令或程序集引用?)
【发布时间】:2020-10-30 02:57:05
【问题描述】:

我开始在 Unity 中使用 PostgreSQL,但我无法使用 npgsql。我使用 NuGet 包管理器安装了它,但我一直有这个错误。有人能帮我吗?我在互联网上搜索了很多,但我无法弄清楚发生了什么。 npgsql 在 Assembly-CSharp 和 Assembly-CSharp-Editor 引用中。

Assembly-CSharp references

Assembly-CSharp-Editor references

我得到这段代码只是作为一个例子,说明如何将 PostgreSQL 与 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;

}

【问题讨论】:

  • 你的 csproj 文件是什么样的?

标签: c# postgresql unity3d


【解决方案1】:

您的代码应位于命名空间内。缺少命名空间可能会导致您拥有命名空间。

【讨论】:

  • 我在互联网上查看了有关命名空间的信息,发现它用于手动创建的类中,对吧?在这种情况下,Npgsql 不是我创建它的,我正在按照我在 Internet 上某些地方看到的有关如何将 PostgreSQL 与 Unity 一起使用的步骤进行操作。
猜你喜欢
  • 2019-12-02
  • 1970-01-01
  • 1970-01-01
  • 2021-08-20
  • 2020-02-19
  • 1970-01-01
  • 2022-06-15
  • 2020-08-29
  • 1970-01-01
相关资源
最近更新 更多