我想将多个数据插入SQL Server数据库,如下所示,但是当我运行此代码时,出现语法错误
关键字' union'附近的语法不正确 ''附近的语法不正确 ''附近的语法不正确 ''附近的语法不正确。
代码:
SqlCommand cmd2 = new SqlCommand("INSERT INTO [rampDB].[dbo].[Answers]([AssessmentID],[questionID],[result],[comment]) SELECT('"
+ assessmentid + "1.1a" + RadioButtonList1.SelectedItem.Value.ToString() + TextBox1.Text + "'union'"
+ "'SELECT'" + assessmentid + "1.1b" + RadioButtonList2.SelectedItem.Value.ToString() + TextBox2.Text + "'union'"
+ "'SELECT'" + assessmentid + "1.1c" + RadioButtonList3.SelectedItem.Value.ToString() + TextBox3.Text + "'union'"
+ "'SELECT'" + assessmentid + "1.1d" + RadioButtonList4.SelectedItem.Value.ToString() + TextBox4.Text + "'union'"
+ "'SELECT'" + assessmentid + "1.1e" + RadioButtonList5.SelectedItem.Value.ToString() + TextBox5.Text
+ "')", sqlConn);
答案 0 :(得分:0)
Relpace"' union'"用这个 - > "联合"
答案 1 :(得分:0)
SQL Server的多插入语法只能使用INSERT
,如下所示:
INSERT INTO rampDB.dbo.Answers (
assessmentID, QuestionId, Result, Comment
) VALUES
( @r1v1, @r1v2, @r1v3, @r1v4 ),
( @r2v1, @r2v2, @r2v3, @r2v4 ),
( @r3v1, @r3v2, @r3v3, @r3v4 )
那就是说,最好的方法是使用带有参数的单个INSERT
,然后为每一行执行。