How to write in the array the column names of the database table (access)?

Connection Code:

OleDbConnection con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;"+ @"Data Source= C:\Users\Person.accdb"); 
  • What do you use to connect to the database? - Grundy
  • string [] arrDBColName = new string [] {"name 1", "name 2", "name 3", "name 4", "name 5", "name 6"}; - LamerXaKer
  • Connect like this: - Catherine
  • OleDbConnection con = new OleDbConnection ("Provider = Microsoft.ACE.OLEDB.12.0;" + @ "Data Source = C: \ Users \ Person.accdb"); - Catherine
  • one
    @YuriiManziuk this solution does not work in Access, as indicated by the link - PashaPash

1 answer 1

 using System.Data; using System.Data.OleDb; using System.Linq; namespace ConsoleApplication52 { class Program { static void Main(string[] args) { using (OleDbConnection con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;" + @"Data Source= C:\Users\Person.accdb")) { con.Open(); string [] columns = con.GetOleDbSchemaTable(OleDbSchemaGuid.Columns, null) .AsEnumerable() .Where(r => r.Field<string>("TABLE_NAME") == "Students") .Select(r => r.Field<string>("COLUMN_NAME")) .ToArray(); } } } } 
  • Thank you very much! Everything is working! - Catherine