My subsystem is receiving SQL queries that are to be visualized in tables. I use DataTable because I have no control over the SQL queries that are to be visualized and they can easily be displayed in a DataGrid.
What I basically have is:
var query = "SELECT 1";
var connection = new SqlConnection(connectionString);
var adapter = new SqlDataAdapter(query, connection);
var table = new DataTable();
adapter.Fill(table);
Console.WriteLine(table.Columns[0].ColumnName);
In this case the column name in the DataTable will be set to Column1
because the input query does not specify a name for the column.
How can I determine if the column name is actually Column1
or generated by the SqlDataAdapter
implementation because of the query not specifying the column name?
My first attempt was to check for Column(N)
, however I realized this is not reliable. What if the query actually specifies the name Column1
? Also I am worried that "Column" may be translated to something else for a localized version of the .NET framework.
I was also thinking of parsing the input SQL to determine how the columns where named, but this quickly becomes difficult and/or error prone.