在用arc engine开发业务系统时,可能会进行开发版本管理这样的一个功能,版本管理中就会涉及到要素集的版本注册的需求。这时候问题就出现了,当sde被其他用户连接时, 要注册版本的要素集就会被其他用户锁定,这时候注册版本就会提示该要素集已被其他用户锁定。所以在进行版本注册的时候,需要将其他用户断开连接,但是自身的连接需要保留。断开用户连接的代码如下:
private void DisconnectUsers(IWorkspace wkspace)
{
try
{
IDatabaseConnectionInfo4 pDCinfo = wkspace as IDatabaseConnectionInfo4;
IEnumUserInfo pEnmUsers = pDCinfo.ConnectedUsers;
IUserInfo pUser = pEnmUsers.Next();
while(pUser!=null)
{
if(!pUser.IsOwnConnection)
{
pDCinfo.DisconnectUser(pUser.SessionID);
}
pUser = pEnmUsers.Next();
}
}
catch(Exception ex)
{
throw ex;
}
}
注意:参数Workspace必须是你的应用程序用的mxd所使用的工作空间对象实例,否则会被断开连接,后续的查看属性表等操作将会报错。获取mxd的工作空间对象实例代码如下:
IHookHelper pHookHelper = new HookHelper();
pHookHelper.Hook = axMapControl.Object;
IFeatureLayer pFlayer =GetFeatureLayerByLayerName(pHookHelper, "LayerName");
IWorkspace _sdeWorkSpace = (pFlayer.FeatureClass as IDataset).Workspace;
/// <summary>
/// 根据图层名称获取当前地图文档中的指定要素图层
/// </summary>
/// <param name="hookHelper">钩子对象</param>
/// <param name="layerName">图层名称</param>
/// <returns></returns>
public IFeatureLayer GetFeatureLayerByLayerName(IHookHelper hookHelper, string layerName)
{
UID pFluid = new UIDClass();
pFluid.Value = "{40A9E885-5533-11d0-98BE-00805F7CED21}";// IFeatureLayer
if (hookHelper.FocusMap.LayerCount == 0)
{
return null;
}
IEnumLayer pLayers = hookHelper.FocusMap.get_Layers(pFluid, true);
ILayer pLayer = pLayers.Next();
while (pLayer != null)
{
if (pLayer.Name == layerName)
{
return pLayer as IFeatureLayer;
}
pLayer = pLayers.Next();
}
return null;
}
"LayerName"参数是mxd里的某一个图层的名称。
接下来进行要素集的版本注册:
var versionobj = _selectDataset as IVersionedObject;
if (verobj != null && (!verobj.IsRegisteredAsVersioned))
{
verobj.RegisterAsVersioned(true);
}
else
{ MessageBox.Show("注册版本失败"); }
完整的代码如下:
private void button_register_Click(object sender, EventArgs e)
{
try
{
if (_selectDataset != null)
{
IHookHelper pHookHelper = new HookHelper();
pHookHelper.Hook = axMapControl.Object;
IFeatureLayer pFlayer =GetFeatureLayerByLayerName(pHookHelper, "LayerName");
IWorkspace _sdeWorkSpace = (pFlayer.FeatureClass as IDataset).Workspace;
//先断开其他用户的连接
DisconnectUsers(_sdeWorkSpace);
var versionobj = _selectDataset as IVersionedObject;
if (verobj != null && (!verobj.IsRegisteredAsVersioned))
{
verobj.RegisterAsVersioned(true);
}
else
{ MessageBox.Show("注册版本失败"); }
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
以上就是一个完整的arcsde版本注册的方法,希望对您有所帮助!
欢迎关注微信公众号“GIS带我奔跑”获取更多GIS前沿开发技术、行业应用和最新资讯。