FogBugz Plugin API > Key-Value Pairs
This article will demonstrate how FogBugz plugins can use the CPluginKeyValueTable interface to abstract the storage and retrieval of basic settings information in the FogBugz "Setting" table. NOTE: Plugin key value pairs are stored in the PluginKeyValue table in the database. Plugins only have access to key value pairs related to that particular plugin.
When to use Key-Value Pairs
Many simple database tasks do not require a relational data model. Quite often, web developers only need a way to store application-level settings across multiple user sessions. For example, FogBugz stores all data regarding the site's SMTP settings as key-value pairs in the "Settings" table, including the SMTP server address, port, and user credentials.
C# Example
An instance of CPluginKeyValueTable can be acquired by calling the GetKeyValueTable() method of CDatabaseApi. From there, you can very easily set and get key-value pairs:
//"api" is an instance of CPluginApi
CPluginKeyValueTable kvt = api.Database.GetKeyValueTable();
kvt.SetValue("FavoriteColor", "Blue");
kvt.SetValue("DaysInTheWeek", Convert.ToString(7));
kvt.Commit();
//Make sure that the commit worked
System.Diagnostics.Debug.Assert(kvt.ContainsKey("FavoriteColor") && kvt.GetValue("FavoriteColor").ToString() == "Blue");
System.Diagnostics.Debug.Assert(kvt.ContainsKey("DaysInTheWeek") && Convert.ToInt32(kvt.GetValue("DaysInTheWeek")) == 7);
//Create an HTML string that shows us all the key / value pairs.
StringBuilder sb = new StringBuilder();
foreach (string key in kvt.Keys)
{
sb.Append(key + ": ");
sb.Append(kvt.GetValue(key));
sb.Append("<br/>");
}