日期:2014-05-20  浏览次数:20750 次

使用SqlCommand的时候, 如何判断insert的Sql是否存在于DB中?
虽然可以用try{...}catch{...}来做,   但是不想这样,   是不是有一个判断的方法,   如果insert语句的主键已经存在于DB之中,   就能跳过当前的insert,   直接运行另外一条insert语句?

用int   result=SqlCommand.ExecuteNonQuery();么?

谢谢

------解决方案--------------------
insert A库.dbo.A表
select * from A库.dbo.B表
where not exists(select 1 from A库.dbo.A表 where id=A库.dbo.B表.id)--id为主键


------解决方案--------------------
if notexists( select id from table where id = "id " )
begin
insert into ...
end