How to List Field's Name in table in Access Using SQL

后端 未结 14 1273
我在风中等你
我在风中等你 2020-12-09 17:15

Can you please let me know if it is possible to list all fields name in a MS Access table?

14条回答
  •  独厮守ぢ
    2020-12-09 17:48

    Give this a go...

        private void Button_OldDDLDump_Click(object sender, EventArgs e)
        {
            string _cnstr = "connectionstringhere";
            OleDbConnection _cn = new OleDbConnection(_cnstr);
            try
            {
                _cn.Open();
                System.Data.DataTable _dt = null;            
                try
                {
                    _dt = _cn.GetSchema("tables");
                    m_msghelper.AppendArray( DataTableHelper.DataTableToString(_dt) );                               
                }
                catch (Exception _ex)
                {
                    System.Diagnostics.Debug.WriteLine(_ex.ToString());
                }
                finally
                {
                    _dt.Dispose();
                }
            }
            catch (Exception _ex)
            {
                System.Diagnostics.Debug.WriteLine(_ex.ToString());
            }
            finally
            {
                _cn.Close();
            }
        }
    

    Helper method to dump the database structure to a string array here..

    public static class DataTableHelper
    {
        public static string[] DataTableToString( System.Data.DataTable dt )
        {
            List _retval = new List();
    
            foreach (System.Data.DataRow row in dt.Rows)
            {
                foreach (System.Data.DataColumn col in dt.Columns)
                {
                    _retval.Add( string.Format("{0} = {1}", col.ColumnName, row[col]) );
                }
                _retval.Add( "============================");
            }
            return _retval.ToArray();
        }
    }
    

提交回复
热议问题