Csharp/C#教程:如果存在则更新其他插入分享


如果存在则更新其他插入

我想知道下一段代码是否正确:

SqlCommand cmd = new SqlCommand( "IF NOT EXISTS(SELECT count(*) from Raspunsuri where id_intrebare=2)" + "Insert INTO Raspunsuri VALUES(@raspuns,@cnp,@data,2,@ip,@idsesiune)" + "else" + "UPDATE Raspunsuri SET raspuns=@raspuns,cod_numeric_personal=@cnp,data_raspuns=@data,id_intrebare=2,ip_user=@ip,id_sesiune=@idsesiune WHERE id_intrebare=2", con); 

所有参数都是正确的,我想插入,但似乎这段代码不做插入或更新。你有任何建议吗?这是一个结合c#的SQL查询..

检查由该命令创建的字符串:某些单词之间需要空格。

  SqlCommand cmd = new SqlCommand("IF NOT EXISTS(SELECT 1 from Raspunsuri where id_intrebare=2)" + " Insert INTO Raspunsuri VALUES(@raspuns,@cnp,@data,2,@ip,@idsesiune)" + " else" + " UPDATE Raspunsuri SET raspuns=@raspuns,cod_numeric_personal=@cnp,data_raspuns=@data,id_intrebare=2,ip_user=@ip,id_sesiune=@idsesiune WHERE id_intrebare=2", con); 

描述

不,因为您选择始终具有值的count

选择一列或*

样品

 SqlCommand cmd = new SqlCommand( "IF NOT EXISTS(SELECT id_intrebare from Raspunsuri where id_intrebare=2) " + "Insert INTO Raspunsuri VALUES(@raspuns,@cnp,@data,2,@ip,@idsesiune) " + "else " + "UPDATE Raspunsuri SET raspuns=@raspuns,cod_numeric_personal=@cnp,data_raspuns=@data,id_intrebare=2,ip_user=@ip,id_sesiune=@idsesiune WHERE id_intrebare=2", con); 

你为什么不使用MERGE命令?

您可以先尝试在SQL Server Management Studio窗口中执行查询。 这将为您提供一种简单的方法来调试这些东西

我认为“else”语句之间缺少空格还确保您已在values部分中提供了所有列

上述就是C#学习教程:如果存在则更新其他插入分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/1028322.html

(0)
上一篇 2022年1月10日
下一篇 2022年1月10日

精彩推荐