当前位置:Gxlcms > mysql > c#获取access所有表名获取指定表所有字段名

c#获取access所有表名获取指定表所有字段名

时间:2021-07-01 10:21:17 帮助过:40人阅读

/// summary /// 取 所有 表名 /// /summary /// returns/returns public Liststring GetTableNameList() { Liststring list = new Liststring(); OleDbConnection Conn = new OleDbConnection(ConnStr); try { if (Conn.State == ConnectionState.Closed) C

       
       /// 
       /// 取所有表名
       /// 
       /// 
       public List GetTableNameList()
       { 
           List list = new List();
           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(); }
       }

       /// 
       /// 取指定所有字段名称
       /// 
       /// 
       public List GetTableFieldNameList(string TableName)
       {
           List list = new List();
           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();
           }
       }

人气教程排行