I have two access tables: users and scores.
Users table has columns: id(auto increment user id), username, password - id is primary key
Scores table has columns: id(user id from users), highScore - has no primary key
In the C# method I take username and score as parameters and I want to insert into the scores table so that in the id field is the id of the user with the username that matches the supplied one.
So far the commands I've tried were:
string insertCommand = @"INSERT INTO scores([id], [highScore])
VALUES((SELECT id FROM users WHERE username = @username), @score);";
This throws: Query input must contain at least one table or query.
So after sniffing around I've found that Access DB is kind of different from usual SQL so I tried using DLookup:
string insertCommand = @"INSERT INTO scores([id], [highScore])
VALUES(DLookup(""id"", ""users"", ""username = '@username'""), @score);";
This goes through but the resulting row is empty. So I basically get an empty row which is not NULL.
I am absolutely certain that the parameters contain values as they are added before the command executes.
command.Parameters.AddWithValue("@username", username);
command.Parameters.AddWithValue("@score", points);
command.CommandText = insertCommand;
command.ExecuteNonQuery();
So I have no idea what I'm doing wrong here. Should I even go at it this way? Could I perhaps somehow JOIN the users and scores tables inside an insert?
""username = @username""instead of""username = '@username'""?Too few parameters. Expected 1.