【发布时间】:2020-10-30 02:57:05
【问题描述】:
我开始在 Unity 中使用 PostgreSQL,但我无法使用 npgsql。我使用 NuGet 包管理器安装了它,但我一直有这个错误。有人能帮我吗?我在互联网上搜索了很多,但我无法弄清楚发生了什么。 npgsql 在 Assembly-CSharp 和 Assembly-CSharp-Editor 引用中。
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