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

AF SDK Getting Started

Connect to a PI Server or AF Database

  • Last UpdatedMay 16, 2023
  • 2 minute read

You can create a list of known AF servers by first initializing an instance of the PISystems collection. The list of all PI Data Archive servers that are known to the client is represented as PIServers. The code below shows how to obtain an object called My AF Server that represents the PI AF server and an object called My PI Data Archive that represents PI Data Archive.

PISystems piSystems = new PISystems();
PISystem assetServer = piSystems["My AF Server"];

PIServers piServers = new PIServers();
PIServer piServer = piServers["My PI Data Archive"];

After you obtain a reference to the PISystem, you can refer to its collection of AFDatabase objects by using the Databases property of piSystem, as shown in the following code:

AFDatabase database = assetServer.Databases["Meters"];

To return a specific AFDatabase, you pass a name to the AFDatabases indexer.

You can obtain additional references from the AFDatabase to other collections such as root elements, categories, templates, and tables. To obtain a specific object from a collection, use the collection's indexer and pass in a name, as shown in the following code.

AFElements rootElements = database.Elements;
AFElement element1 = rootElements["Meter1"];
AFCategories attributeCategories = database.AttributeCategories;
AFElementTemplates elementTemplates = database.ElementTemplates;
AFTables tables = database.Tables;

Notice that the object hierarchy corresponds very closely to the hierarchy you see in PI System Explorer. Remember to use PI System Explorer as a guide for illustrating object relationships within AF SDK.

Note that there is no explicit call to a Connect() method. AF SDK connects to the PI AF database automatically as required. This automatic connection is called an implicit connection. To make an explicit connection, you can call Connect() on a PISystem instance. Implicit and explicit connections are discussed in more detail in the AF SDK Online Reference.

Example

static void PrintRootElements(AFDatabase database)
{
Console.WriteLine("Print Root Elements: {0}", database.Elements.Count);
foreach (AFElement element in database.Elements)
{
Console.WriteLine(" {0}", element.Name);
}

Console.WriteLine();
}

TitleResults for “How to create a CRG?”Also Available in