C # / MySQL error of multiple select statements in insert statement
I have a little problem inserting from 2 different tables in C # into a MySql database
Here is my code where the error occurs
command.CommandText =
@"INSERT INTO personen (Name, Surname, GenderID, BirthplaceID)
VALUE @Name, @Surname, (SELECT GenderID FROM gender WHERE Gender = @GenderID),
(SELECT BirthplaceID FROM place WHERE placename = @BirthplaceID);";
When I replace the "@" operators and just fill in the name, it works correctly. So I want to know where the problem is.
Also the problem is not with the parameters as in some of the answers, because I added them (see below).
insertname = data.Name;
insertsurname = data.Surname;
insertgenderid= data.Gender;
insertbirthplaceid= data.Birthplace;
command.Parameters.AddWithValue("@Name", insertname);
command.Parameters.AddWithValue("@Surname", insertsurname);
command.Parameters.AddWithValue("@GenderID", insertgenderid);
command.Parameters.AddWithValue("@BirthplaceID", insertbirthplaceid);
command.ExecuteNonQuery();
+3
source to share
1 answer
If the problem is what you are calling the header:
Here is a sqlfiddle of this confirmation now (tables are all int just because the syntax is what we go for, not types)
First, it must be VALUES not VALUES. And secondly, VALUES must be enclosed in parentheses.
command.CommandText =
@"INSERT INTO personen (Name, Surname, GenderID, BirthplaceID)
VALUES (@Name, @Surname,
(SELECT GenderID FROM gender WHERE Gender = @GenderID),
(SELECT BirthplaceID FROM place WHERE placename = @BirthplaceID)
);";
+3
source to share