jtymann jtymann - 3 months ago 13
C# Question

How to read Result of SELECT * from Joined Tables With Duplicate Column Names in .NET

I am a PHP/MYSQL developer, slowly venturing into the realm of C#/MSSQL and I am having a problem in C# when it comes to reading an MSSQL query that joins two tables.

Given the two tables:





And given the query

SELECT * FROM TableA,TableB WHERE TableA.b_id = TableB.id;

Now in C# I normally read query data in the following fashion:

SqlDataReader data_reader= sql_command.ExecuteReader();

Except in this case I need to differentiate from TableA's name column, and TableB's name column.

In PHP I would simply ask for the field "TableA.name" or "TableB.name" accordingly but when I try something like


in C#, my code errors out. Now I know this can probably be solved with a Google search or a search on this site, but I am having the hardest time coming up with the appropriate search term to find an answer.

So could someone please point me in the write direction, or give me a suggestion as to how to read a query on multiple tables in C#?


The result set only sees the returned data/column names, not the underlying table. Change your query to something like

SELECT TableA.Name as Name_TA, TableB.Name as Name_TB from ...

Then you can refer to the fields like this: