4

I'm trying to read the column names of a table "Streets" in an Access database by opening an OleDbConnection. I call GetOleDbSchemaTable but I can't seem to figure out how to get at my columns.

I'd like to use .NET 3.5 framework if possible.

Tony Toews
  • 7,850
  • 1
  • 22
  • 27
patrick
  • 16,091
  • 29
  • 100
  • 164

2 Answers2

12
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
    connection.Open();

    DataTable tableColumns = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Columns, new object[] { null, null, "Streets", null }));
    foreach (DataRow row in tableColumns.Rows)
    {
        var columnNameColumn = row["COLUMN_NAME"];
        var dateTypeColumn = row["DATA_TYPE"];
        var ordinalPositionColumn = row["ORDINAL_POSITION"];
        ...
    }
}
Tom Mayfield
  • 6,235
  • 2
  • 32
  • 43
vc 74
  • 37,131
  • 7
  • 73
  • 89
  • @vc 74 That doesn't work for me, I get "DataTable does not have public def of IEnum" so can't even use the foreach, then when I put it in a for {} it says COLUMN_NAME is not a member of TABLE "Tables" – patrick Oct 13 '10 at 14:49
0

This works for me, puts column names of table street in a list

OleDbCommand mdbCommand = new OleDbCommand("SELECT * FROM Streets", mdbConnection);
OleDbDataReader myReader = mdbCommand.ExecuteReader(CommandBehavior.KeyInfo);
DataTable  schemaTable = myReader.GetSchemaTable();
List<String> columnNameList = new List<string>();
foreach (DataRow myField in schemaTable.Rows)
{
foreach (DataColumn myProperty in schemaTable.Columns)
{
if (myProperty.ColumnName == "ColumnName") columnNameList.Add(myField[myProperty].ToString());
}

}
patrick
  • 16,091
  • 29
  • 100
  • 164
  • 1
    Yes, but you are opening a query that can potentially take time to execute (longer than a metadata query), and you will probably not get all the info you can get from GetOleDbSchemaTable – vc 74 Oct 13 '10 at 15:08