For a current project I'm working on, we need to script the user profile synchronisation connection so that we can deploy it on our different servers. To do this, you need to take a few different steps.

First you have to open the user profile manager context

// Open the central administration
SPSite centralAdminSite = SPAdministrationWebApplication.Local.Sites[0]; 

// Open the user profile manager context
SPServiceContext context = SPServiceContext.GetContext(centralAdminSite);
UserProfileManager userProfileManager = new UserProfileManager(context);
UserProfileConfigManager userProfileConfigManager = new UserProfileConfigManager(context); 

Then set up the parmaters for the connection

string connectionName = "My Connection"; // 
string systemName = "UserProfileLOBSystemInstance"; // The LobSystemInstance name
string entityName = "UserProfileEntity"; // The Entity name
string filterName = ""; // The filter ??
string entityNamespace = "UserProfiles.BdcModel1"; // Namespace of the project
string profilePropertyName = "AccountName"; // The unique SharePoint property to map the BDC to
string mappedAttribute = "ID"; // The unique BDC property 

List<DSMLAttribute> dsmlAttributes = new List<DSMLAttribute>();
DSMLAttribute attr = null; 

// Loop through all items from some sort of resource, E.G. an XML file
// The DSML attributes seem to be required so the connection knows these properties are available for mapping
// and that these properties can be mapped to a BDC field
foreach (var item in items)
{
   attr = new DSMLAttribute(); // create a new attribute
   attr.Indexible = bool.Parse(item.indexible);
   attr.Name = item.name; // the name of de BDC property
   attr.ID = item.id;
   attr.SingleValued = bool.Parse(item.singledValue);
   attr.Syntax = (DSMLSyntax)Enum.Parse(typeof(DSMLSyntax), item.syntax);
   dsmlAttributes.Add(attr);
} 

and finally create the connection

try
{
 // Create the new user profile synchronisation connection
 userProfileConfigManager.ConnectionManager.AddBusinessDataCatalogConnection(
         connectionName, systemName, entityName, entityNamespace, profilePropertyName, 
                filterName, mappedAttribute, dsmlAttributes);
}
catch (SystemException sex)
{
 // In some way, sometimes there is thrown an exception when nothing was wrong
}
catch (Exception ex)
{ 

}

To figure all this stuff out, I needed a lot of reflection and patience. Hope this gives you the right start to work with user profile sync connections.

Have fun!