简体   繁体   English

C# 如果在 SQL 更新中可用,否则插入

[英]C# if available in SQL Update else insert

I have checked many posts on Stackoverflow but I am still stuck with the query below.我已经检查了 Stackoverflow 上的许多帖子,但我仍然坚持下面的查询。 I am a beginner learning C# and would request help.我是学习 C# 的初学者,会请求帮助。

Error: Incorrect Syntax near keyword ELSE错误:关键字 ELSE 附近的语法不正确

I am trying to check if the Question1 or Question2 if either of them is selected the below should be executed.我正在尝试检查是否选择了 Question1 或 Question2 中的任何一个,则应执行以下操作。 Also other time it gave me an error @answer2 not defined.还有其他时候它给了我一个错误@answer2 未定义。

string query = @"IF EXISTS(SELECT * FROM dbo.tbl_pkt_Answers_Submitted  WHERE (Questions = @Question1 or Questions = @Question2))

    UPDATE dbo.tbl_pkt_Answers_Submitted 
    SET AnswersSubmitted = @Answer1
    WHERE Questions = @Question1;

    UPDATE dbo.tbl_pkt_Answers_Submitted 
    SET AnswersSubmitted = @Answer2
    WHERE Questions = @Question2;

    ELSE

    INSERT INTO dbo.tbl_pkt_Answers_Submitted(Questions, AnswersSubmitted)   
    VALUES (@Question1, @Answer1), (@Question2, @Answer2);";

SqlCommand cmd = new SqlCommand(query, con);

cmd.Parameters.Add("@Question1", lblQuestion1.Text);
cmd.Parameters.Add("@Question2", lblQuestion2.Text);

if (rdq1a1.Checked)
{
    cmd.Parameters.Add("@Answer1", rdq1a1.Text);
}

if (rdq1a2.Checked)
{
    cmd.Parameters.Add("@Answer1", rdq1a2.Text);
}

if (rdq1a3.Checked)
{
    cmd.Parameters.Add("@Answer1", rdq1a3.Text);
}

if (rdq1a4.Checked)
{
    cmd.Parameters.Add("@Answer1", rdq1a4.Text);
}

if (rdq2a1.Checked)
{
    cmd.Parameters.Add("@Answer2", rdq2a1.Text);
}

if (rdq2a2.Checked)
{
    cmd.Parameters.Add("@Answer2", rdq2a2.Text);
}

if (rdq2a3.Checked)
{
    cmd.Parameters.Add("@Answer2", rdq2a3.Text);
}

if (rdq2a4.Checked)
{
    cmd.Parameters.Add("@Answer2", rdq2a4.Text);
}

con.Open();
cmd.ExecuteNonQuery();

MessageBox.Show("Your Data has been saved");
con.Close();

The 'true' part of the IF statement contains multiple SQL statements, you must surround these statements within BEGIN and END IF 语句的“true”部分包含多个 SQL 语句,您必须将这些语句括在 BEGIN 和 END 中

string query = @"IF EXISTS(SELECT * FROM dbo.tbl_pkt_Answers_Submitted  WHERE (Questions = @Question1 or Questions = @Question2))
                 BEGIN
                    UPDATE dbo.tbl_pkt_Answers_Submitted 
                    SET AnswersSubmitted = @Answer1
                    WHERE Questions = @Question1;

                    UPDATE dbo.tbl_pkt_Answers_Submitted 
                    SET AnswersSubmitted = @Answer2
                    WHERE Questions = @Question2;
                 END
                 ELSE
                    INSERT INTO dbo.tbl_pkt_Answers_Submitted(Questions, AnswersSubmitted) 
                    VALUES(@Question1, @Answer1), (@Question2, @Answer2);";

The main problem here is that your UPDATE statements after the IF are not enclosed with BEGIN and END .这里的主要问题是IF之后的UPDATE语句没有用BEGINEND括起来。 Not using those only works with single batch statements, not multiple.不使用那些只适用于单个批处理语句,而不是多个。 So your statement should look like:所以你的陈述应该是这样的:

IF EXISTS(..)
BEGIN
UPDATE 1
UPDATE 2
END
ELSE
yadayada

It appears that your Insert statement looks suspicious.看来您的 Insert 语句看起来很可疑。

INSERT INTO dbo.tbl_pkt_Answers_Submitted(Questions, AnswersSubmitted) 
VALUES(@Question1, @Answer1)";

also, it isnt surrounded by begin and end statement.此外,它没有被开始和结束语句包围。 It should be它应该是

If exists ...
begin
...
end
else 
insert

You have multiple statements in your if condition being true.您的 if 条件为真中有多个语句。 If there is one statement then it is okay, but with multiple statements, you need to use BEGIN...END如果只有一个语句就可以了,但是如果有多个语句,则需要使用BEGIN...END

IF EXISTS(SELECT *
          FROM dbo.tbl_pkt_Answers_Submitted
          WHERE (Questions = @Question1 OR Questions = @Question2))
    BEGIN
        UPDATE dbo.tbl_pkt_Answers_Submitted
        SET AnswersSubmitted = @Answer1
        WHERE Questions = @Question1;

        UPDATE dbo.tbl_pkt_Answers_Submitted
        SET AnswersSubmitted = @Answer2
        WHERE Questions = @Question2;
    END
ELSE
    INSERT INTO dbo.tbl_pkt_Answers_Submitted(Questions, AnswersSubmitted)
    VALUES (@Question1, @Answer1),
           (@Question2, @Answer2);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM