Search code examples
c#sqlms-accessoledbdatabase-metadata

How to get index names through SQL or OLEDB from .NET?


How can I get index names for an Access table using OLEDB or SQL ?

(I searched a lot on the internet in the last two days and did not find anything related to this issue.)


Solution

  • The OleDbConnection has a method called GetSchema that takes a string to select the collection of metadata that you want to retrieve.

    Some of the possible values for the string parameter are Tables, Columns, Indexes

    using(OleDbConnection cnn = new OleDbConnection("...."))
    {
        cnn.Open();
        DataTable schemaIndexes = cnn.GetSchema("Indexes");
        foreach(DataRow row in schemaIndexes.Rows)
        {
           Console.WriteLine("Table={0}, Index={1} on field={2}",
               row.Field<string>("TABLE_NAME"),
               row.Field<string>("INDEX_NAME"),
               row.Field<string>("COLUMN_NAME"));
        }
    }