5

Imagine we have two tables in the database, user (FK id_role) and role (PK role). We need to read information about user and his privileges.

I use the following SQL statement to perform query:

SELECT * 
FROM [user] 
INNER JOIN role ON [user].id_role = role.id 
WHERE login = @login

After executing, I try to read values in reader using string indexer: reader[string name].

The problem which I need to resolve is repeating names: both user and role contain, e.g., the field id, which I am able to read for user (using reader["id"]), but not able to read for role (using reader["role.id"]).

The property FieldCount returns 12, which means that all required fields were read (user contains 6 fields, so does role).

Do I have a possibility to read columns by name in this case? Or using two queries or SQL 'as' operator in the only way?

2
  • 4
    You need to define column aliases in your SELECT - or read by ordinal position (Reader[index]) - but that of course is prone to other problems (like User table gets one more property and then your orders are all off by 1) - and those could be solved by not using SELECT * - but explicitly listing all the columns you really need and want to select from those two tables... Commented Apr 8, 2012 at 18:46
  • 1
    Of course reading by position is uncomfortable a bit... Probably aliases is the answer. But I'm confused why datareader doesn't add table name before columns. Commented Apr 8, 2012 at 18:51

4 Answers 4

12

You could always select the columns explicitly and alias them:

SELECT user.id AS user_id, user.*, role.id AS role_id, role.* 
FROM [user] 
INNER JOIN role ON [user].id_role = role.id 
WHERE login = @login

Then select them with reader["user_id"] and reader["role_id"].

Sign up to request clarification or add additional context in comments.

3 Comments

Just a curiosity to know: does it show user_id column two times? and same for the role_id column?? Thanks.
@sarwar026, yeah, in what I wrote there both ID columns would be selected twice.
It's a nice way and probably the easiest one, thanks! I guess the best way to avoid such problems is to provide tables' columns with unique names.
1

The only robust way is to use AS in the SQL-query to make sure your column names/aliases are unique... otherwise you would to access the fields by index (0 / 1 / 2 etc.) instead of by name.

Comments

1
SELECT role.id as RoleId, user.id as UserId
FROM [user] 
INNER JOIN role ON [user].id_role = role.id 
WHERE login = @login

Using AS

reader["RoleId"];

1 Comment

Is there a solution not using AS?
1

Of course, you should select columns by name, but you need to do it like:

SELECT u.id as uid, r.id as rid, ... , FROM [user] u INNER JOIN role r ON [user].id_role = role.id WHERE login = @login

With , ... , I want to select the remaining column of both the tables.

Finally, use reader["uid"], reader["rid"] etc.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.