using Npgsql.dll to access PostgreSQL server

After downloading the dlls
from The 5 Best Budget Gaming Chairs of 2022 | Pro Gaming Foundry
as directed from Redirecting…

unity2 seems ok when

using Npgsql

in a c# script

however
when

IDbConnection dbcon;
       dbcon = new NpgsqlConnection(connectionString);

i get

any ideas what’s not happening here ?

perhaps some guidelines from the gurus at OTEE …
ie which distribution to use;

Npgsql2.0beta1
Npgsql1.0.1

etc

 using System;
 //using System.Data;   ## REMOVE THIS
 using Npgsql;
 
 public class Test
 {
    public static void Main(string[] args)
    {
       string connectionString =
          "Server=localhost;" +
          "Database=test;" +
          "User ID=postgres;" +
          "Password=fun2db;";
       // 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 firstname, lastname " +
           "FROM employee";
       dbcmd.CommandText = sql;
       //IDataReader reader = dbcmd.ExecuteReader(); ## CHANGE THIS TO
       NpgsqlDataReader reader = dbcmd.ExecuteReader();
      while(reader.Read()) {
            string FirstName = (string) reader["firstname"];
            string LastName = (string) reader["lastname"];
            Console.WriteLine("Name: " +
                 FirstName + " " + LastName);
       }
       // clean up
       reader.Close();
       reader = null;
       dbcmd.Dispose();
       dbcmd = null;
       dbcon.Close();
       dbcon = null;
    }
 }

and now Unity can query a PostgreSQL server :smile:

1 Like

This warms the heart of an old SQL junkie!

d.

I used this and Unity server crashes when reach the ExecuteReader

Use Npgsql2.0beta2.

This saved my day! Thanks!

I just ran more tests about Npgsql issues on Unity 4.3.3:

  • Npgsql 1.0.1 and 2.0RTM works fine in both Editor and player
  • Any Npgsql >=2.0.1 version works fine in the player, but completely crashes the Unity editor when trying to play in the editor

That’s unfortunate that we can’t get the goodies from the latest Npgsql with Postgres 9.3 support, but at least the “old” versions are pretty robust.

Actually the problem is with Unity’s default assembly finding algorithm
First it searches assemblies in "C:\Program Files\Unity\Editor\Data\Managed" than in "C:\Program Files\Unity\Editor\Data\Mono\lib\mono\2.0" than in some other folder and it seems it searches project folder path the last.
Unity is shipped with it’s own Npgsql.dll in "C:\Program Files\Unity\Editor\Data\Mono\lib\mono\2.0" that is why the editor crashes. Your assembly is compiled against your own Npgsql and is run with Unity’s.

So you should place your own Npgsql.dll to "C:\Program Files\Unity\Editor\Data\Managed" and Mono.Security.dll too i think. Also I recommend to build Npgsql from source without System.DirectoryServices dependency. It is used only for integrated security feature which can be commented out from the source.

2 Likes

Where would it go on Mac OS Yosemite?

I delved into and found to do this in Mac OS Yosemite you need to right click on Unity and Select Show Package Contents… If you can’t find the Unity app for whatever reason, and it is in your dock, hold command down and left click it. After that Navigate to Contents>Frameworks>Managed and that is where you place the npgsql.dll and Mono.Security.dll which is the same directory UnityEngine.dll is in. I haven’t recompiled it without the System.Directory.Services but this is something I will be doing as well. Thank you Yatagarasu!