Monday, July 30, 2012

Geomedia code to get the GRecordset's Connection object

When developing driving GeoMedia type applications, I find it convenient to be able to determine from which database connection a feature record set is from. The following C# code snippet is a private method that given a collection of Connection object and a GRecordset object, it returns the Connection object of the record set; or null if it could not find a match.


using PClient = Intergraph.GeoMedia.PClient;
//...etc...
private PClient.Connection GetRecordSetConnection(PClient.Connections conns, PClient.GRecordset rs)
{
PClient.GFields flds = null;
PClient.Connections conns = null;
PClient.GDatabase db = null;
PClient.Connection conn = null;
 
//Get the recordset's GFields collection object
flds = rs.GFields;
 
//Loop through the GeoMedia documents' list of connections
foreach (PClient.Connection cn in conns)
{
//Ignore closed connections
if (cn.Status != PClient.ConnectionConstants.gmcStatusClosed)
{
//Get the current connection's database object
db = (PClient.GDatabase) cn.Database;
//If the recordset's field database name matches the
//connection's database name, then we have found the right connection object
if (db.Name.Equals(flds[0].SourceDatabase))
{
conn = cn;
break;
}
}
}
//Free up memory used by the GeoMedia COM objects
if (flds != null) 
Marshal.FinalReleaseComObject(flds);
if (conns != null) 
Marshal.FinalReleaseComObject(conns);
if (db != null) 
Marshal.FinalReleaseComObject(db);

return conn;
}

No comments: