Retrieve column names using metadata  
Author Message
heavenlycharmus





PostPosted: Visual C# General, Retrieve column names using metadata Top

I am implementing an application that needs to quickly retrieve a list of column names of a table for an ODBC connection. Is there a way to use metadata to achieve the goal so that I don't need to run a query on the entire table Thanks.


Visual C#9  
 
 
cverdon





PostPosted: Visual C# General, Retrieve column names using metadata Top

Hi,

If you're using table adapters and dataset you can call the table adapter's FillShema method on the table you're interrested in. Next you just have to iterate on the table's column:

DataSet ds = new DataSet();

ds.Tables.Add( "MyTable" );

 

OdbcConnection conn = new OdbcConnection( connString );

 

OdbcDataAdapter adapter = new OdbcDataAdapter();

adapter.SelectCommand = new OdbcCommand(

                  "SELECT * FROM SomeTable", conn );

adapter.FillSchema( ds, SchemaType.Source, "MyTable" );

 

MessageBox.Show( String.Format( "Columns: {0}  Keys: {1}", ds.Tables["MyTable"].Columns.Count, ds.Tables["MyTable"].PrimaryKey.Length ) );

 

foreach(DataColumn c in ds.Tables["MyTable"].Columns) {

MessageBox.Show( String.Format( "Name: {0}  Type: {1}",

                     c.ColumnName, c.DataType.ToString() ) );

}

Regards,
Charles