Reputation: 1950
What's the simplest way to get a string array of database field names? I tried using SHOW COLUMNS
but I couldn't get it to work.
I'm currently doing it this way:
private List<string> GetDBFieldNames()
{
List<string> dbFieldNames = new List<string>();
try {
System.Data.SqlClient.SqlConnection con = d.connectDB();
String query = "select * from my_table";
SqlDataAdapter cmd = new SqlDataAdapter(query, con);
DataTable dt = new DataTable();
cmd.Fill(dt);
foreach(DataColumn column in dt.Columns) {
dbFieldNames.Add(column.ColumnName);
}
}
catch (Exception ex) {}
return dbFieldNames;
}
Upvotes: 2
Views: 1225