求教:希望实现递归打印数据字典内容,程序出错。还请高手指点一二。
public class testNod{
Database db = HostApplicationServices.WorkingDatabase;
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
public void Test()
{
using (Transaction trans = db.TransactionManager.StartTransaction())
{
ObjectId nodId = db.NamedObjectsDictionaryId;
nodeSearch(nodId);
trans.Commit();
}
}
private void nodeSearch(ObjectId nodId)
{
Transaction trans = db.TransactionManager.StartTransaction();
DBDictionary nod = (DBDictionary)trans.GetObject(nodId, OpenMode.ForWrite);
DbDictionaryEnumerator dictEnum = nod.GetEnumerator();
ObjectId objId = new ObjectId();
while (dictEnum.MoveNext())
{
objId = dictEnum.Value;
string objName = dictEnum.Key;
ed.WriteMessage("\n" + objId.ToString() + " " + objName);
if (trans.GetObject(nodId, OpenMode.ForWrite) is DBDictionary)
{
nodeSearch(objId);
}
else
{
break;
}
}
}
} 本帖最后由 jdjb 于 2011-4-24 00:51 编辑
我调试好的代码。 还有两点不明请大家指教。
1.未调用trans.commit为何不发生错误,什么时候需要调用commit什么时候不需要。
2.是否有高手为小弟对trans实现机制稍作指点。
public class testNod
{
Database db = HostApplicationServices.WorkingDatabase;
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
public void Test()
{
ObjectId nodId = db.NamedObjectsDictionaryId;
nodeSearch(nodId);
}
private void nodeSearch(ObjectId nodId)
{
using (Transaction trans = db.TransactionManager.StartTransaction())
{
DBDictionary nod = trans.GetObject(nodId, OpenMode.ForWrite) as DBDictionary;
if (nod != null)
{
DbDictionaryEnumerator dictEnum = nod.GetEnumerator();
ObjectId objId = new ObjectId();
while (dictEnum.MoveNext())
{
objId = dictEnum.Value;
string objName = dictEnum.Key;
ed.WriteMessage("\n" + objId.ToString() + " " + objName);
nodeSearch(objId);
}
}
}
}
}
本帖最后由 sieben 于 2011-4-24 09:13 编辑
1,数据词典DBDictionary里面的可能是词典DBDictionary,也可能是扩展记录Xrecord
2,我也不大明白"什么时候需要调用commit什么时候不需要",我简单理解为只读不需要,写入则需要
(后注:这里回的是二楼)
本帖最后由 sieben 于 2011-4-24 09:14 编辑
private void nodeSearch(ObjectId nodId)
{
DBDictionary nod = (DBDictionary)trans.GetObject(nodId, OpenMode.ForWrite);
if (trans.GetObject(nodId, OpenMode.ForWrite) is DBDictionary)
{
nodeSearch(objId);
}
-------------------------------
1,这里怀疑变量写错了,if (trans.GetObject(nodId, OpenMode.ForWrite) is DBDictionary) 实是想写if (trans.GetObject(objId, OpenMode.ForWrite) is DBDictionary)
2,不管是objId 还是 nodId,你都重复打开了
(后注:这里回的是一楼,回二楼时没有看到一楼)
本帖最后由 chmenf087 于 2011-4-24 16:51 编辑
.commit() 和.abort两个是对应的commit表示事务所有在commit之前的操作都被存储,而.abort则表示回滚,放弃前面所有对数据库的操作,因此没有需要在数据库中存储的操作时就不需要.commit()
页:
[1]