Y2theZ
Y2theZ

Reputation: 10412

How can I get the column names from an Excel sheet?

I have a database table. Is there a query that gets the names of the columns in a table?

Example:

Name || Age || Gender
Tom  || 30  || male
Kate || 20  || Female

I want a query to get the column names: Name , Age, Gender

Thanks you

edit: Sorry about some missing info:

I am using an OleDB connection in C# to read data from an excel sheet

Upvotes: 2

Views: 6070

Answers (3)

Nitin Walawalkar
Nitin Walawalkar

Reputation: 39

i wanted to upload the columns of a table from Excel, so I uploaded the table in a Dataset and then checked the column names and inserted in database. you can get an idea from the below code

for (int i = 0; i < dsUpload.Tables[0].Columns.Count; i++)
                    {
                        if (dsUpload.Tables[0].Columns[i].ColumnName.ToString() != "")
                        {
                            // Assigning ColumnName
                            objExcelUpload.ColumnName = dsUpload.Tables[0].Columns[i].ColumnName.ToString().Replace("'", "''").Replace("<", "&lt;").Replace(">", "&gt;").Trim();
                            if (!objExcelUpload.ifColumnNameExist("insert"))
                            {
                                if (objExcelUpload.ColumnName != "")
                                {
                                    objExcelUpload.insertColumns();    
                                }

                            }
                            else
                            {
                                ErrorLabel.Text = "The column name already exists. Please select a different name.";
                                return;
                            }

                        }
                    }

Here ds Upload is a dataset name and the code useful for you is

objExcelUpload.ColumnName = dsUpload.Tables[0].Columns[i].ColumnName.ToString()

which is checked in a loop of all the available columns

for (int i = 0; i < dsUpload.Tables[0].Columns.Count; i++)

Let me know if you need any clarification :-)

Upvotes: 0

Andomar
Andomar

Reputation: 238256

You can retrieve a list of columns in a table like:

select * from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = 'YourTable'

INFORMATION_SCHEMA is an ISO standard, so it works on most databases.

Upvotes: 1

MDEV
MDEV

Reputation: 10838

I believe you're after the SHOW COLUMNS query.

SHOW COLUMNS FROM mytable

More info: http://dev.mysql.com/doc/refman/5.0/en/show-columns.html

Upvotes: 0

Related Questions