Search code examples
c#postgresql.net-5npgsql

How to set timeout value in NpgsqlConnection?


From Documentation: https://www.npgsql.org/doc/connection-string-parameters.html Docs

Connection string:"...;Timeout=1200";

Code:

public IDbConnection Connection
{
    get
    {
        return new NpgsqlConnection() { ConnectionString = this.config.GetConnectionString("PostgresConn") };
    }
}

...

    using (IDbConnection dbConnection = Connection)
    {                
        return dbConnection.ExecuteScalar<int>(query, new { OP = op }) > 0;
    }

Error message:

Couldn't set timeout (Parameter 'timeout')


Solution

  • I get a more explicit error message specifying the timeout must be less than 1024

    System.ArgumentException: Couldn't set timeout (Parameter 'timeout') ---> System.ArgumentOutOfRangeException: Timeout must be between 0 and 1024 (Parameter 'value')

    and this is confirmed in the code here and here: