0

我有一个这样的sql命令

SqlCommand cmd = new SqlCommand(
"IF NOT EXISTS(SELECT english from dic where english='"+e_word+"') " +
"Insert INTO dic VALUES('"+e_word+"',N'"+b_word+"') " +
"else " +
"UPDATE dic SET english='"+e_word+"',bangla=N'"+b_word+"' WHERE  english='"+e_word+"'",con);   


      cmd.ExecuteNonQuery();

但我得到了像这样的错误

Incorrect syntax near 's'.
Incorrect syntax near 's'.

我该如何解决这个问题??????

4

1 回答 1

4

自由贸易区:

SqlCommand cmd = new SqlCommand(@"
IF NOT EXISTS(SELECT english from dic where english=@e_word)
Insert INTO dic (english, bangla) VALUES(@e_word,@b_word)
else UPDATE dic SET bangla=@b_word WHERE english=@e_word",con);   

cmd.Parameters.AddWithValue("e_word", e_word);   
cmd.Parameters.AddWithValue("b_word", b_word);
cmd.ExecuteNonQuery();

(实际上,我通常会做我的 upserts 有点不同,但我不想重写整个事情)

于 2013-01-31T12:52:08.873 回答