I was wondering how I could get the columns of a database table and store each of them in a string or string array. I have the following code but I believe it does not work.
In addition to previous answers here is another way. For me it works faster (not much) than alternatives with DataReader. "WHERE FALSE" tells to bring no data.
Note that there are no need to iterate through DataTable.Rows as the result DataTable already describes the target table's schema (see the DataTable.Columns), not the schema of the current DataReader results
DataTable GetDataTableScheme(string tableName)
{
var table = new DataTable();
using (var connection = new MySqlConnection(*[ConnectionString]*))
using (var dataAdapter = new MySqlDataAdapter($"SELECT * FROM {tableName} WHERE FALSE", connection))
{
// Adds additional info, like auto-increment
dataAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
dataAdapter.Fill(table);
}
return table;
}