Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fetch column names for specific table

I want to fetch all the column names for specific table..

I am using msaccess and C# .net 2008.

like image 588
krunal shah Avatar asked Sep 23 '10 02:09

krunal shah


1 Answers

You can fetch schema information for a given query through OleDb using the SchemaOnly CommandBehavior and the GetSchemaTable method, as follows:

var conStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=NWIND.mdb";
using (var con = new OleDbConnection(conStr))
{
    con.Open();
    using (var cmd = new OleDbCommand("select * from Suppliers", con))
    using (var reader = cmd.ExecuteReader(CommandBehavior.SchemaOnly))
    {
        var table = reader.GetSchemaTable();
        var nameCol = table.Columns["ColumnName"];
        foreach (DataRow row in table.Rows)
        {
            Console.WriteLine(row[nameCol]);
        }
    }
}
like image 153
Nathan Baulch Avatar answered Sep 22 '22 09:09

Nathan Baulch