书籍是全世界的营养品。生活里没有书籍,就好像没有阳光;智慧里没有书籍,就好像鸟儿没有翅膀。
DBMNG数据库管理与应用
当前位置:首页 > Access > 技术手册
c# asp.net 获取access所有表名 获取指定表所有字段名

/// <summary>
    /// 取所有表名
    /// </summary>
    /// <returns></returns>
    public List<string> GetTableNameList()
    {
        List<string> list = new List<string>();
        OleDbConnection Conn = new OleDbConnection(ConnStr);
        try
        {
            if (Conn.State == ConnectionState.Closed)
                Conn.Open();
            DataTable dt = Conn.GetSchema("Tables");
            foreach (DataRow row in dt.Rows)
            {
                if (row[3].ToString() == "TABLE")
                    list.Add(row[2].ToString());
            }
            return list;
        }
        catch (Exception e)
        { throw e; }
        finally { if (Conn.State == ConnectionState.Open) Conn.Close(); Conn.Dispose(); }
    }
 
    /// <summary>
    /// 取指定表所有字段名称
    /// </summary>
    /// <returns></returns>
    public List<string> GetTableFieldNameList(string TableName)
    {
        List<string> list = new List<string>();
        OleDbConnection Conn = new OleDbConnection(ConnStr);
        try
        {
            if (Conn.State == ConnectionState.Closed)
                Conn.Open();
            using (OleDbCommand cmd = new OleDbCommand())
            {
                cmd.CommandText = "SELECT TOP 1 * FROM [" + TableName + "]";
                cmd.Connection = Conn;
                OleDbDataReader dr = cmd.ExecuteReader();
                for (int i = 0; i < dr.FieldCount; i++)
                {
                    list.Add(dr.GetName(i));
                }
            }
            return list;
        }
        catch (Exception e)
        { throw e; }
        finally
        {
            if (Conn.State == ConnectionState.Open)
                Conn.Close();
            Conn.Dispose();
        }
    }



本站文章内容,部分来自于互联网,若侵犯了您的权益,请致邮件chuanghui423#sohu.com(请将#换为@)联系,我们会尽快核实后删除。
关于我们 - 版权声明 - 服务条款 - RSS订阅 - 工作机会 - 友情链接 - 联系我们 - 在线留言
Copyright © 2006-2018 DBMNG.COM All Rights Reserved. Powered by DEVSOARTECH   豫ICP备11002312号-2

豫公网安备 41010502002439号