3

I've got a number of SQL statements executing in my application like so:

using (SqlConnection connection = new SqlConnection(connectionString))
{
   connection.Open();

   using (SqlDataAdapter adapter = new SqlDataAdapter(statement, connection))
   {
      adapter.Fill(dtResults);
   }
}

Each statement can have an AS clause, for example: SELECT id AS 'Customer ID' FROM table. Is it possible to ignore the AS part of the statement, essentially sending the real column name to the DataTable?

11
  • 1
    Why don't you just fix the input?
    – Jon Skeet
    Feb 23, 2013 at 19:10
  • @JonSkeet Because I've got hundreds of SQL statements in the system, which would result in manually changing them all :/
    – Dot NET
    Feb 23, 2013 at 19:11
  • 2
    Do it then. Fix the problem instead of putting a sticking plaster on it.
    – Jon Skeet
    Feb 23, 2013 at 19:21
  • @JonSkeet The thing is that it's hundreds of queries "in theory". What I'm working on is a query generator, and basically builds SQL code based on user input. By default the SQL statements employ an AS clause so as to display the respective column's metadata. However, when exporting results to Excel, I would not like to display columns based on metadata. Thus, my options are to generate another SQL statement without AS clauses (basically going over all my SQL generation code classes), or find some other way.
    – Dot NET
    Feb 23, 2013 at 19:25
  • 1
    It sounds like fixing your SQL generation classes to have "with aliases" and "without aliases" it the way to go then.
    – Jon Skeet
    Feb 23, 2013 at 19:26

3 Answers 3

1

I'm always keen on fixing a problem as close to the source as I can, rather than fixing it "downstream" so to speak.

In this case, it sounds like the problem is that your SQL generators aren't as flexible as they need to be. You need to change them so that you can control whether or not they emit the AS aliasing within the query. That's likely to be more reliable than post-processing the "incorrect" query - and means that your code can clearly express what you're trying to achieve.

2
  • This is what I've done, and I have to agree - it was the better choice :) Thanks for the suggestion and +1 !
    – Dot NET
    Feb 24, 2013 at 17:43
  • 1
    @DotNET: Sometimes it just takes another person reassuring you that the "slower but better" long term fix is worthwhile compared with the quick and dirty hack that would hurt eventually ;)
    – Jon Skeet
    Feb 24, 2013 at 17:51
0

Short answer is no. What about changing the column names in dtResults after it's filled, though?

Can you describe the problem you are trying to solve more specifically?

2
  • Basically I'm building an SQL generator, which can generate SQL statements with column names based on meta-data within the database, when being displayed in a Datagrid. However, users can export the data, and on doing so it would have to use the real values.
    – Dot NET
    Feb 23, 2013 at 19:13
  • The only thing I can think of is if you were able to SET SHOWPLAN_XML on, run the statement and get the column mappings, then run it again with showplan_xml off. I don't know if this would work in your particular case as obviously in general it wouldn't help (select price * quantity as total) but for your purposes it might.
    – Mark Sowul
    Feb 23, 2013 at 19:20
0

What I do is have a table of tables with column names and display names and then in the DataTable I use the display name as the ColumnName.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.