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 UpdatedJan 12, 2026
  • 4 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();

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}

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}

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();

See Also

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