sql查询从数据库中获取数据

时间:2013-01-31 12:47:15

标签: c# sql sql-server-2008

我有像这样的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'.

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

1 个答案:

答案 0 :(得分:4)

FTFY:

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();

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