How to connect to Postgresql database using Xamarim

Asked

Viewed 157 times

0

I’m creating a mobile application using the Postgresql database, but I can’t open the connection:

My code:

public class PgSql
{
    private string connString = "Server=192.168.1.5;Port=5432;User ID=postgres;Password=Kalisba987;Database=LotusERP;";
    private NpgsqlConnection conn;

    public string connect()
    {
        Thread thread = new Thread();
        thread.Start();
        string retorno;
        conn = new NpgsqlConnection(connString);
        try
        {
            this.conn.Open();
            retorno = "Conexão aberta com o banco PostgreSql";
        }
        catch (NpgsqlException ex)
        {
            retorno = ex.Message;
        }
        finally
        {
            this.conn.Close();
        }
        return retorno;
    }
}

The error of every time on the line

conn = new NpgsqlConnection(connString);

And the following message appears:

Unhandled Exception: System.Typeinitializationexception: The type initializer for 'System.Collections.Generic.List`1' threw an Exception

  • 1

    You are stifling the exception in the code within the catch and making it impossible for the real message to be shown to you. This is a small example of what can happen when using the exception mechanism in a bad way. Remove the block catch and let the exception do its job, then edit your question and add the actual error message.

  • It is not possible, only through a web service.

  • There’s no way to make that mistake at this point in the code unless you have a bug in NpgsqlConnection(), which I doubt. I don’t know why create a thread that is not being used, I do not know how much this is interfering in deceiving what is taking place.

  • It is not possible to use Npgsqlconnection() in Xamarin, only when it supports . NET Standard. https://github.com/npgsql/npgsql/issues/451

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.