当前位置: 代码迷 >> ASP.NET >> 删除数据时,如何先判断数据是否存在后再删除
  详细解决方案

删除数据时,如何先判断数据是否存在后再删除

热度:9311   发布时间:2013-02-25 00:00:00.0
删除数据时,怎么先判断数据是否存在后再删除?
删除数据时,怎么先判断数据是否存在后再删除?求高人指点!

------解决方案--------------------------------------------------------
那就先查询看在不在,再删除呗
------解决方案--------------------------------------------------------
那你就先查询下数据库,然后判断一下,不就行了
------解决方案--------------------------------------------------------
select count(1) from table where ...
 查询是否存在
------解决方案--------------------------------------------------------
很简单的不用判断都可以, 你直接 删除, 用 SqlCommand。ExecuteNonQuery()方法 这个 方法有个返回值 , 这个值 会告诉你 数据库中几行受影响, 0行 受影响 就 返回0, 这样 你就知道 数据 是否存在, 成功删除就是 存在 否则就不存在, 不用先去查询,再删除, 这样 效率低很多, 
下面是列子 提供参考:
C# code
SqlConnection conn=new SqlConnection("连接语句");SqlCommand comm=new Command("删除语句",conn);int result=comm.ExecuteNonQuery();return result;
------解决方案--------------------------------------------------------
C# code
SqlConnection conn=new SqlConnection("连接字符串");SqlCommand comm=new Command("select count(*) from 表 where……",conn);int result=comm.ExecuteNonQuery();return result;if(result>0){ SqlCommand cmd=new Command("delete from 表 where……",conn); int result=cmd.ExecuteNonQuery(); return result1; if(result>0){  Response.Write("<script>alert('删除成功!');</script>");}else{Response.Write("<script>alert('删除失败!');</script>");}}else{  Response.Write("<script>alert('数据不存在!');</script>");}
  相关解决方案