I was looking around on SO on how to retrieve the column name and I tried out some solutions but when I use this method, for example, I recieve these column names and not my actual column namnes (ID, Status, Title etc.):
- TABLE_CATALOG
- TABLE_SCHEMA
- TABLE_NAME
TABLE_TYPE
using (SqlConnection connection = new SqlConnection(this.ConnectionString)) { System.Data.SqlClient.SqlConnectionStringBuilder builder = new System.Data.SqlClient.SqlConnectionStringBuilder(); builder.ConnectionString = this.ConnectionString; string server = builder.DataSource; string database = builder.InitialCatalog; connection.Open(); DataTable schema = connection.GetSchema("Tables"); Tables = new List<Table>(); foreach (DataRow row in schema.Rows) { /* Add Table */ Table t = new Table(); string tableName = row[2].ToString(); t.Name = tableName; /* Add columns */ //DataTable dtCols = connection.GetSchema("Columns", new[] { "StarTrackerDB", null, "dbo.Tickets" }); t.Columns = new List<Column>(); foreach (DataColumn column in row.Table.Columns) { Column c = new Column(); c.Name = column.ColumnName; t.Columns.Add(c); } Tables.Add(t); } }
EDIT:
I want to retrieve it in C# i.e. not execute an SQL query string in my code.
EDIT2
Current output:
- TABLE_CATALOG
- TABLE_SCHEMA
- TABLE_NAME
- TABLE_TYPE
Expected output:
- ID
- Status
- Title
etc. the column names in the tables. the string tableName
is set correctly.