/// <summary> /// 获取表属性字段描述字典 /// </summary> /// <param name="dbPath">数据文件路径</param> /// <param name="tbName">表名</param> /// <returns></returns> public static Dictionary <string, string> GetFieldDescriptions(string dbPath, string tbName) { try { const int dbUseJet = 2; dao.Workspace DAOWorkspace; dao.Database DAODatabase; dao.DBEngine DAODBEngine = new dao.DBEngine(); //创建一个工作区 DAOWorkspace = DAODBEngine.CreateWorkspace("WorkSpace", "Admin", "", dbUseJet); //打开数据库 DAODatabase = DAOWorkspace.OpenDatabase(dbPath, false, false, null); dao.TableDef DAOTable; Dictionary <string, string> fieldCaptionDic = new Dictionary <string, string>(); // 表对象 DAOTable = DAODatabase.TableDefs[tbName]; foreach (dao.Field field in DAOTable.Fields) { //读取 UserName 字段的 “标题”属性,如果标题没有设置,则会抛出异常。 //如果标题不存在,我们就添加一个标题 String descriptionText; try { descriptionText = field.Properties["Description"].Value.ToString(); } catch { descriptionText = field.Name; } fieldCaptionDic.Add(field.Name, descriptionText); } DAODatabase.Close(); return(fieldCaptionDic); } catch { return(null); } }
/// <summary> /// 获取表属性字段描述字典 /// </summary> /// <param name="dbPath">数据文件路径</param> /// <param name="tbName">表名</param> /// <returns></returns> public static Dictionary<string, string> GetFieldDescriptions(string dbPath, string tbName) { try { const int dbUseJet = 2; dao.Workspace DAOWorkspace; dao.Database DAODatabase; dao.DBEngine DAODBEngine = new dao.DBEngine(); //创建一个工作区 DAOWorkspace = DAODBEngine.CreateWorkspace("WorkSpace", "Admin", "", dbUseJet); //打开数据库 DAODatabase = DAOWorkspace.OpenDatabase(dbPath, false, false, null); dao.TableDef DAOTable; Dictionary<string, string> fieldCaptionDic = new Dictionary<string, string>(); // 表对象 DAOTable = DAODatabase.TableDefs[tbName]; foreach (dao.Field field in DAOTable.Fields) { //读取 UserName 字段的 “标题”属性,如果标题没有设置,则会抛出异常。 //如果标题不存在,我们就添加一个标题 String descriptionText; try { descriptionText = field.Properties["Description"].Value.ToString(); } catch { descriptionText = field.Name; } fieldCaptionDic.Add(field.Name, descriptionText); } DAODatabase.Close(); return fieldCaptionDic; } catch { return null; } }