I have a little problem to insert from 2 different table's in C# to MySql database
我有个小问题要从c#中的两个不同的表插入到MySql数据库中
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 "@" statements and just fill in the name's, it works properly. So I want to know where the problem is.
当我替换“@”语句并只填写名称时,它就正常工作了。我想知道问题出在哪里。
Also the problem ain't the parameters like some answers said, cause i've 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();
1 个解决方案
#1
3
If the problem is what you list as the title:
如果问题是你列出的标题:
Here is the sqlfiddle of this validating now (tables are all int just because syntax is what we are going for not, types)
这是现在验证的sqlfiddle(表都是int型的,因为语法是我们要使用的不是类型)
First, it should be VALUES not VALUES. And second, the VALUES must be wrapped 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)
);";
#1
3
If the problem is what you list as the title:
如果问题是你列出的标题:
Here is the sqlfiddle of this validating now (tables are all int just because syntax is what we are going for not, types)
这是现在验证的sqlfiddle(表都是int型的,因为语法是我们要使用的不是类型)
First, it should be VALUES not VALUES. And second, the VALUES must be wrapped 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)
);";