Set ApplicationName runtime / code

Set ApplicationName runtime / code

Postby chris901 » Sun 07 Aug 2016 18:03

Hello,

the PgSqlConnection itself doesn't have a Property for ApplicationName.

However when doubleclicking on the component in the designer i can set it in the Initialization group.

I am wondering how I can set the ApplicationName in runtime / code.

I don't want to set it by string replacing / adding the ConnectionString but am looking for a more elegant way.

Also i would like to change the Pooling settings in the same manner.

Thanks.
chris901
 
Posts: 64
Joined: Wed 20 Jul 2016 04:21

Re: Set ApplicationName runtime / code

Postby Pinturiccio » Thu 11 Aug 2016 14:11

You are right, PgSqlConnection does not have a property to set the ApplicationName connection string parameter, because this parameter must be set before opening a connection. After a connection is opened, this parameter can be set only via the connection string. The connection will be closed after changing a connection string. The same is true for the Pooling connection string parameter.
Pinturiccio
Devart Team
 
Posts: 2025
Joined: Wed 02 Nov 2011 09:44

Re: Set ApplicationName runtime / code

Postby chris901 » Thu 11 Aug 2016 19:14

I bought dotConnect for Postgres Professional and looked at the setter for the other properties of PgSqlConnection.

I tried creating a descendant component of PgSqlConnection and access the ApplicationName and Pooling with it.

However i do not have access to the internal classes.

Code: Select all

public bool ApplicationName
{
    get
    {
        PgSqlConnectionOptions connectionOptions = this.ConnectionOptions as PgSqlConnectionOptions;
        if (connectionOptions == null)
             return false;

       return connectionOptions.Pooling;
    }

    set
    {
        PgSqlConnectionOptions connectionOptions = this.ConnectionOptions as PgSqlConnectionOptions;
        if (connectionOptions == null)
             return;

          connectionOptions.Pooling = value;
    }
}





Is it possible to access the classes PgSqlConnectionOptions and ConnectionOptions from an inherited component?

,
chris901
 
Posts: 64
Joined: Wed 20 Jul 2016 04:21

Re: Set ApplicationName runtime / code

Postby Pinturiccio » Fri 12 Aug 2016 13:32

chris901 wrote:Is it possible to access the classes PgSqlConnectionOptions and ConnectionOptions from an inherited component?

There is no possibility to get access to the PgSqlConnectionOptions class, cause it is a private class. Besides, it’s pointless, because changing its property value won’t lead to SET APPLICATION_NAME execution, because this action is performed when a connection opens.

You can implement the ApplicationName property by executing the "SET APPLICATION_NAME" and "SHOW APPLICATION_NAME" statements

Here is an example of code, implementing it:
Code: Select all
public string ApplicationName
{
   get
   {
      PgSqlCommand command = this.CreateCommand();
      command.CommandText = "SHOW application_name";
      return (string)command.ExecuteScalar();
   }
   set
   {
      PgSqlCommand command = this.CreateCommand();
      command.CommandText = "SET APPLICATION_NAME = '" + value + "'";
      command.ExecuteNonQuery();
   }
}


As for the Pooling connection string parameter, it cannot be changed after opening the connection. You can change its value by modifying the connection string, but your connection will be closed when changing the connection string. The property can be implemented in the following way:

Code: Select all
public bool Pooling
{
   get
   {
      PgSqlConnectionStringBuilder sb = new PgSqlConnectionStringBuilder(this.ConnectionString);
      return sb.Pooling;
   }
   set
   {
      PgSqlConnectionStringBuilder sb = new PgSqlConnectionStringBuilder(this.ConnectionString);
      sb.Pooling = value;
      this.ConnectionString = sb.ConnectionString;
   }
}
Pinturiccio
Devart Team
 
Posts: 2025
Joined: Wed 02 Nov 2011 09:44

Re: Set ApplicationName runtime / code

Postby chris901 » Sun 14 Aug 2016 05:45

Thanks, I was looking for something like this.
chris901
 
Posts: 64
Joined: Wed 20 Jul 2016 04:21


Return to dotConnect for PostgreSQL