- /// <summary>
- /// 获取或创建根词典
- /// </summary>
- /// <param name="name"></param>
- /// <returns></returns>
- public static DBDictionary GetOrCreateDictionary(string name)
- {
- var db = HostApplicationServices.WorkingDatabase;
- using (Transaction tr = db.TransactionManager.StartTransaction())
- {
- var dict = db.NamedObjectsDictionaryId.GetObject<DBDictionary>(OpenMode.ForWrite);
- if (!dict.Contains(name))
- {
- var ndict = new DBDictionary();
- dict.SetAt(name, ndict);
- tr.AddNewlyCreatedDBObject(ndict, true);
- tr.Commit();
- return ndict;
- }
- return dict;
- }
- }
- /// <summary>
- /// 创建词典并设置 XRecord
- /// 2015.03.24
- /// </summary>
- /// <param name="name">子词典名</param>
- /// <param name="key">Xrecord 名</param>
- /// <param name="rb">数据</param>
- /// <returns></returns>
- public static void CreateDictionary(string name, string key, ResultBuffer rb)
- {
- var db = HostApplicationServices.WorkingDatabase;
- using (var tr = db.TransactionManager.StartTransaction())
- {
- DBDictionary subDictionary;
- var nod = db.NamedObjectsDictionaryId.GetObject<DBDictionary>(OpenMode.ForWrite);
- try
- {
- subDictionary = nod.GetAt(name).GetObject<DBDictionary>(OpenMode.ForWrite);
- }
- catch (Exception)
- {
- subDictionary = new DBDictionary();
- nod.SetAt(name, subDictionary);
- tr.AddNewlyCreatedDBObject(subDictionary, true);
- subDictionary = nod.GetAt(name).GetObject<DBDictionary>(OpenMode.ForWrite);
- }
- Xrecord xrec;
- try
- {
- xrec = (Xrecord) tr.GetObject(subDictionary.GetAt(key), OpenMode.ForWrite);
- xrec.Data = rb;
- xrec.Dispose();
- }
- catch (Exception)
- {
- subDictionary.UpgradeOpen();
- xrec = new Xrecord {Data = rb};
- subDictionary.SetAt(key, xrec);
- tr.AddNewlyCreatedDBObject(xrec, true);
- }
- tr.Commit();
- }
- }
|