We can use the SELECT COLUMN_NAME SQL query to get column names of a table in MySQL. Here is the complete syntax:
SELECT column_name FROM information_schema.columns WHERE table_name=@t_name
This query returns all the column names of table 't_name".
Here are the steps to execute this query in ASP.NET.
Step 1: Download MySql.Data.dll and add referance of it. Learn here for more details, How to Connect with MySQL Database in C#.
Step 2: Import the following namespaces:
  1. using MySql.Data.MySqlClient;
  2. using MySql.Data;
Step 3: Here is a function to get columnlist by passing tablename and connectionstring in string formats.
  1. public DataTable ColumnList(string connection, string tablename, bool identity)
  2. {
  3. MySqlConnection conn = new MySqlConnection(connection);
  4. DataTable dt = new DataTable();
  5. MySqlCommand cmd = new MySqlCommand("select COLUMN_NAME as 'name' from information_schema.columns where table_name=@t_name", conn);
  6. cmd.Parameters.AddWithValue("@t_name", tablename);
  7. MySqlDataAdapter da = new MySqlDataAdapter(cmd);
  8. da.Fill(dt);
  9. return dt;
  10. }
Step 4: Now, dt is a DataTable that has columns. You can read column names from there.