PIServer.Connect Method (Boolean, IWin32Window, AFConnectionPreference, Boolean)
- Last UpdatedNov 18, 2025
- 4 minute read
- PI System
- AF SDK 2024 R2
- Developer
Open a connection to the PI Data Archive using the specified connection preference
with a credential prompt if necessary to allow sending and retrieving data
and return an indication that dialog was canceled.
Namespace: OSIsoft.AF.PI
Assembly: OSIsoft.AFSDK (in OSIsoft.AFSDK.dll) Version: 3.1.1.1182
Syntax
public void Connect( bool autoPrompt, IWin32Window owner, AFConnectionPreference preference, out bool wasCanceled )
Public Sub Connect ( autoPrompt As Boolean, owner As IWin32Window, preference As AFConnectionPreference, <OutAttribute> ByRef wasCanceled As Boolean ) Dim instance As PIServer Dim autoPrompt As Boolean Dim owner As IWin32Window Dim preference As AFConnectionPreference Dim wasCanceled As Boolean instance.Connect(autoPrompt, owner, preference, wasCanceled)
public: void Connect( bool autoPrompt, IWin32Window^ owner, AFConnectionPreference preference, [OutAttribute] bool% wasCanceled )
member Connect : autoPrompt : bool * owner : IWin32Window * preference : AFConnectionPreference * wasCanceled : bool byref -> unit
Parameters
- autoPrompt
- Type: SystemBoolean
If , then a dialog will be displayed to prompt for user credentials if the connection to the server fails. The choice of PIAuthenticationMode in the prompt dialog is only displayed if the AllowExplicit option is set in the AuthenticationOptions setting. When this option is not set, then only WindowsAuthentication is allowed. - owner
- Type: System.Windows.FormsIWin32Window
The handle to the window that owns the credential prompt dialog. If , then the owner will be the desktop. This parameter is only used if autoPrompt is and there is a security error when attempting to connect to the PIServer. - preference
- Type: OSIsoft.AFAFConnectionPreference
The connection preference to use when determining which member of the PICollective is selected for the connection. The default preference can be obtained from Preference property of the this object's ConnectionInfo. The Preference setting will be updated to this value after a successful connection. - wasCanceled
- Type: SystemBoolean
Returns if the credential dialog prompt was displayed and it was canceled. Otherwise, is returned.
Exceptions
| Exception | Condition |
|---|---|
| PIConnectionException | A connection to the PI Data Archive server cannot be made. |
| PIVersionNotSupportedException | This exception will be generated when attempting to connect to a PIServer with using WindowsAuthentication and the server does not support this mode of authentication. |
Remarks
If an application accesses a property or invokes a method that requires server
access, an implicit connection is performed. When a connection is no longer needed,
it can be closed using the Disconnect method.
Examples
// Get the PIServers collection for the current user and default PIServer. PIServer myPIServer = new PIServers().DefaultPIServer; // Simple connect. myPIServer.Connect(); myPIServer.Disconnect(); // Connect and display a credential prompt dialog if current user login fails. // Only available in .Net Framework AFSDK // myPIServer.Connect(true, null); // myPIServer.Disconnect(); try { // Connect using a specified credential. NetworkCredential credential = new NetworkCredential("guest", String.Empty); myPIServer.Connect(credential); } catch (Exception ex) { // Expected exception since credential needs a valid user name and password. Console.WriteLine(ex.Message); }
' Get the PIServers collection for the current user and default PIServer. Dim myPIServer As PIServer = New PIServers().DefaultPIServer ' Simple connect. myPIServer.Connect() myPIServer.Disconnect() ' Connect and display a credential prompt dialog if current user login fails. ' Only available in .Net Framework AFSDK ' myPIServer.Connect(True, Nothing) ' myPIServer.Disconnect() Try ' Connect using a specified credential. Dim credential As NetworkCredential = New NetworkCredential("guest", String.Empty) myPIServer.Connect(credential) Catch ex As Exception ' Expected exception since credential needs a valid user name and password. Console.WriteLine(ex.Message) End Try
No code example is currently available or this language may not be supported.
No code example is currently available or this language may not be supported.