Please ensure Javascript is enabled for purposes of website accessibility
Powered by Zoomin Software. For more details please contactZoomin

AF SDK Reference

Connecting to a PI Data Archive

  • Last UpdatedNov 18, 2025
  • 7 minute read
Connecting to a PI Data Archive

This topic contains the following sections:

There are several methods for connecting to a PI Data Archive. The AF SDK will handle caching objects and connections to the server, therefore you should only close a connection when you are through using the server for a long period of time. This is different from typical programming against a database where you open a connect, get the data, and then close the connection.

When programming in a multi-user environment where the user identity could change between calls, you must not re-use any AF SDK objects between calls and get new objects starting with the PIServers collection each time. The AF SDK maintains a cache of objects and connections per user which is determined by the obtained PIServers collection for the current user.

Implicit Connection

This example demonstrates the method for connecting to a PI Data Archive server implicitly.

1// Get the PIServer once and share within the application.
2if (staticPIServer == null)
3    staticPIServer = new PIServers().DefaultPIServer;
4
5// Do operation, a connection will automatically be created if needed for this user.
6PIPoint myPoint = PIPoint.FindPIPoint(staticPIServer, piPointName);
7AFValue myValue = null;
8if (myPoint != null)
9    myValue = myPoint.CurrentValue();
 1' Get the PIServer once and share within the application.
 2If (sharedPIServer Is Nothing) Then
 3    sharedPIServer = New PIServers().DefaultPIServer
 4End If
 5' Do operation, a connection will automatically be created if needed for this user.
 6Dim myPoint As PIPoint = PIPoint.FindPIPoint(sharedPIServer, piPointName)
 7Dim myValue As AFValue = Nothing
 8If (Not (myPoint) Is Nothing) Then
 9    myValue = myPoint.CurrentValue()
10End If

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.

Explicit Connection

This example demonstrates the normal method for connecting to a PI Data Archive server explicitly.

 1// Get the PIServers collection for the current user and default PIServer.
 2PIServer myPIServer = new PIServers().DefaultPIServer;
 3
 4// Simple connect.
 5myPIServer.Connect();
 6myPIServer.Disconnect();
 7
 8
 9// Connect and display a credential prompt dialog if current user login fails.
10// Only available in .Net Framework AFSDK
11// myPIServer.Connect(true, null);
12// myPIServer.Disconnect();
13
14try
15{
16    // Connect using a specified credential.
17    NetworkCredential credential = new NetworkCredential("guest", String.Empty);
18    myPIServer.Connect(credential);
19}
20catch (Exception ex)
21{
22    // Expected exception since credential needs a valid user name and password.
23    Console.WriteLine(ex.Message);
24}
 1' Get the PIServers collection for the current user and default PIServer.
 2Dim myPIServer As PIServer = New PIServers().DefaultPIServer
 3
 4' Simple connect.
 5myPIServer.Connect()
 6myPIServer.Disconnect()
 7
 8' Connect and display a credential prompt dialog if current user login fails.
 9' Only available in .Net Framework AFSDK
10' myPIServer.Connect(True, Nothing)
11' myPIServer.Disconnect()
12
13Try
14    ' Connect using a specified credential.
15    Dim credential As NetworkCredential = New NetworkCredential("guest", String.Empty)
16    myPIServer.Connect(credential)
17Catch ex As Exception
18    ' Expected exception since credential needs a valid user name and password.
19    Console.WriteLine(ex.Message)
20End 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.

Collective Connection

This example demonstrates the method for connecting to a PI Data Archive collective.

 1// Get the PIServers collection for the current user and default PIServer.
 2PIServer myPIServer = new PIServers().DefaultPIServer;
 3
 4// Set default for all connections to be based upon collective member's priority.
 5PIConnectionInfo.DefaultPreference = AFConnectionPreference.Any;
 6PICollectiveMember myMember;
 7
 8// Check if default PIServer is a Collective.
 9if (myPIServer.Collective != null)
10{
11    // Simple connect will use Default Preference.
12    myPIServer.Connect();
13    myPIServer.Disconnect();
14
15    // Connect specifying that Primary is required and display a credential
16    // Connect and display a credential prompt dialog if current user login fails.
17    // Only available in .Net Framework AFSDK
18    // myPIServer.Connect(true, null, AFConnectionPreference.RequirePrimary);
19    // myPIServer.Disconnect();
20
21    // Connect specifying that Primary is required and display a credential
22    // Connect and display a credential prompt dialog if current user login fails.
23    // Only available in .Net Framework AFSDK
24    // Connect to a specific collective member and display a credential
25    // Prompt dialog if current user login fails.
26    // PICollectiveMember myMember = myPIServer.Collective.Members[0];
27    // myMember.Connect(true, null);
28    // myPIServer.Disconnect();
29
30    try
31    {
32        // Connect to a specific collective member using a specified credential.
33        myMember = myPIServer.Collective.Members[0];
34        NetworkCredential credential = new NetworkCredential("guest", String.Empty);
35        myMember.Connect(credential);
36    }
37    catch (Exception ex)
38    {
39        // Expected exception since credential needs a valid user name and password.
40        Console.WriteLine(ex.Message);
41    }
42
43    // Connect to a multipe members of a collective simultaneously using ConnectDirect
44    if (myPIServer.Collective.Members.Count > 1)
45    {
46        PIServer myMemberAsPIServer1 = myPIServer.Collective.Members[0].ConnectDirect();
47        PIServer myMemberAsPIServer2 = myPIServer.Collective.Members[1].ConnectDirect();
48        myMemberAsPIServer1.Disconnect();
49        myMemberAsPIServer2.Disconnect();
50    }
51
52    myPIServer.Disconnect();
53}
54else
55{
56    Console.WriteLine("PIServer '{0}' is not a collective. No connections were made.", myPIServer.Name);
57}
 1' Get the PIServers collection for the current user and default PIServer.
 2Dim myPIServer As PIServer = New PIServers().DefaultPIServer
 3
 4' Set default for all connections to be based upon collective member's priority.
 5PIConnectionInfo.DefaultPreference = AFConnectionPreference.Any
 6
 7Dim myMember As PICollectiveMember
 8
 9' Check if default PIServer is a Collective.
10If (myPIServer.Collective IsNot Nothing) Then
11    ' Simple connect will use Default Preference.
12    myPIServer.Connect()
13    myPIServer.Disconnect()
14
15    ' Connect specifying that Primary is required and display a credential
16    ' Prompt dialog if current user login fails.
17    ' Only available in .Net Framework AFSDK
18    ' myPIServer.Connect(True, Nothing, AFConnectionPreference.RequirePrimary)
19    ' myPIServer.Disconnect()
20
21    ' Connect to a specific collective member and display a credential
22    ' Prompt dialog if current user login fails.
23    ' Only available in .Net Framework AFSDK
24    ' myMember = myPIServer.Collective.Members(0)
25    ' myMember.Connect(True, Nothing)
26    ' myPIServer.Disconnect()
27
28    Try
29        ' Connect to a specific collective member using a specified credential.
30        myMember = myPIServer.Collective.Members(0)
31        Dim credential As NetworkCredential = New NetworkCredential("guest", String.Empty)
32        myMember.Connect(credential)
33    Catch ex As Exception
34        ' Expected exception since credential needs a valid user name and password.
35        Console.WriteLine(ex.Message)
36    End Try
37End If

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.

Multi-User Connection

This example demonstrates the method for connecting to a PI Data Archive server using an implicit connection in a multi-user environment. In this environment, it is important not to re-use any AF objects if the user identity could have changed. Instead, you need to get a new PIServers collection before accessing any PI objects.

1// Get the PIServers collection for the current user and default PIServer.
2PIServer myPIServer = new PIServers().DefaultPIServer;
3
4// Do operation, a connection will automatically be created if needed for this user.
5PIPoint myPoint = PIPoint.FindPIPoint(myPIServer, piPointName);
6AFValue myValue = null;
7if (myPoint != null)
8    myValue = myPoint.CurrentValue();
1' Get the PIServers collection for the current user and default PIServer.
2Dim myPIServer As PIServer = New PIServers().DefaultPIServer
3
4' Do operation, a connection will automatically be created if needed for this user.
5Dim myPoint As PIPoint = PIPoint.FindPIPoint(myPIServer, piPointName)
6Dim myValue As AFValue = Nothing
7If (Not (myPoint) Is Nothing) Then
8    myValue = myPoint.CurrentValue()
9End If

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.

See Also

In This Topic
Related Links
TitleResults for “How to create a CRG?”Also Available in