.. index: Advanced MapGuide Topics ======================== Using Transactions ------------------ A transaction encapsulates a unit of work performed on a Feature Source that will either wholly succeed or wholly fail. Transactions ensure the Feature Source is kept in a consistent state whether the operation succeeded or the operation failed. In the MapGuide API, the **MgTransaction** class represents the transaction to be performed in a Feature Source. The following example shows how to use a transaction over a series of feature operations: **.net (C#)** .. highlight:: csharp .. code-block:: csharp //This code fragment assumes you have imported the OSGeo.MapGuide namespace MgTransaction trans = null; MgFeatureService featSvc = (MgFeatureService)siteConnection.CreateService(MgServiceType.FeatureService); MgResourceIdentifier featureSourceId = new MgResourceIdentifier("Library://Data/Test.FeatureSource"); try { //Start the transaction, all changes below will not take effect until the transaction is committed trans = featSvc.StartTransaction(featureSourceId); //This does a SQL insert on the feature source featSvc.ExecuteSql(featureSourceId, "INSERT INTO TABLE1(ID, NAME) VALUES(123, 'Test'), trans); //This does a SQL update on the feature source featSvc.ExecuteSql(featureSourceId, "UPDATE TABLE1 SET NAME = 'Joe Bloggs' WHERE ID = 32", trans); //This does a SQL delete on the feature source featSvc.ExecuteSql(featureSourceId, "DELETE FROM TABLE1 WHERE ID = 456, trans); //Committing the transaction. The above changes will take effect trans.Commit(); } catch (MgException ex) { //Rollback the transaction. Any changes that the above SQL commands would've //made are discarded if (trans != null) trans.Rollback(); } .. note:: Transactions are only supported by certain FDO providers. Check the provider capabilities of the Feature Source to determine if Transactions are available. Describing a Feature Source --------------------------- A Feature Source represents a connection to a FDO data store. FDO data stores are modeled in the following fashion: * A data store contains 1 or more Feature Schemas * A Feature Schema contains 1 or more Class Definitions * A Class Definition contains 1 or more Property Definitions The **MgFeatureService** class provides various methods for describing such aspects of a Feature Source The following example lists the schema names of a Feature Source **.net (C#)** .. highlight:: csharp .. code-block:: csharp //This code fragment assumes you have imported the OSGeo.MapGuide namespace MgFeatureService featSvc = (MgFeatureService)siteConnection.CreateService(MgServiceType.FeatureService); MgResourceIdentifier featureSourceId = new MgResourceIdentifier("Library://Data/Test.FeatureSource"); MgStringCollection schemaNames = featSvc.GetSchemas(featureSourceId); Response.Write("

Schema Names:

"); The following example lists the class names for a given schema **.net (C#)** .. highlight:: csharp .. code-block:: csharp //This code fragment assumes you have imported the OSGeo.MapGuide namespace MgFeatureService featSvc = (MgFeatureService)siteConnection.CreateService(MgServiceType.FeatureService); MgResourceIdentifier featureSourceId = new MgResourceIdentifier("Library://Data/Test.FeatureSource"); MgStringCollection classNames = featSvc.GetClasses(featureSourceId, schemaName); Response.Write("

Class Names for " + schemaName + ":

"); Given a schema name and class name, the following example gets the matching class definition and writes out information about the class definition **.net (C#)** .. highlight:: csharp .. code-block:: csharp //This code fragment assumes you have imported the OSGeo.MapGuide namespace MgFeatureService featSvc = (MgFeatureService)siteConnection.CreateService(MgServiceType.FeatureService); MgResourceIdentifier featureSourceId = new MgResourceIdentifier("Library://Data/Test.FeatureSource"); MgClassDefinition classDef = featSvc.GetClassDefinition(featureSourceId, schemaName, className); Response.Write("

Name: " + classDef.GetName() + "

"); Response.Write("

Description: " + classDef.GetDescription() + "

"); Response.Write("

Default Geometry Property: " + classDef.GetDefaultGeometryPropertyName() + "

"); MgPropertyDefinitionCollection identityProps = classDef.GetIdentityProperties(); MgPropertyDefinitionCollection clsProps = classDef.GetProperties(); Response.Write("

Class Properties (* are identity):

"); Response.Write("