c#读取access中指定表的字段名及其描述

  /// <summary>
        /// 获取指定表名的列名与描述注释
        /// </summary>
        /// <param name="mdbFilePath"></param>
        /// <param name="tableName"></param>
        /// <returns></returns>
  public static Hashtable GetTableFieldsDisFromMdb(string mdbFilePath, string tableName)
        {
            Hashtable tableFields = new Hashtable();
            try
            {
                ADOX.CatalogClass cat = new ADOX.CatalogClass();
                string sAccessConnection = strConnRoot + mdbFilePath;
                ADODB.Connection cn = new ADODB.Connection();
                cn.Open(sAccessConnection, null, null, -1);
                cat.ActiveConnection = cn;
                ADOX.Table tb = cat.Tables[tableName];
                if (tb!=null)
                {
                    foreach (ADOX.Column col in tb.Columns)
                    {
                        string colName = col.Name.ToString();
                        string colDis = "";
                        foreach (ADOX.Property pro in col.Properties)
                        {
                            if (pro.Name == "Description")
                            {
                                if (pro.Value != null) colDis = pro.Value.ToString();
                                break;
                            }
                        }
                        tableFields.Add(colName, colDis);
                    }
                }
                cat = null;
                cn.Close();
            }
            catch (System.Exception ex)
            {
                return tableFields;
            }
            return tableFields;
        }
 

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。