Connect to a Data Archive or AF database
- Last UpdatedFeb 19, 2026
- 2 minute read
- PI System
- AF SDK 3.2.0
- PI Server
You can create a list of known AF servers by first initializing an instance of the PISystems collection. The list of all Data Archive servers 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 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;
The object hierarchy closely matches the hierarchy shown in PI System Explorer. Remember, use PI System Explorer as a guide for illustrating object relationships within the AF SDK.
Tip: There is no explicit call to a Connect() method. The AF SDK connects to a 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 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();
}