当前位置:编程学习 > C#/ASP.NET >>

C#里写insert into语句如何加where条件

我在C#里写了一条插入语句,想加上where Stationname=1391111111。如何加呢。必须要满足这个条件后才能插入。
sql += "insert into PlanMssion_ShangChuan(Station,JHour,JMinite,MissionParm1,MissionParm2,PressValue,YulvValue) values('" + ws.Dtuid + "','" + ws.Shi + "','" + ws.Fen + "','" + ws.BianPinSheZhi + "','" + ws.XiaoDuSheZhi + "','" + ws.YaLi + "','" + ws.YuLv + "');"; --------------------编程问答-------------------- 看不懂,想加就加吗 --------------------编程问答--------------------
if not exists
--------------------编程问答-------------------- 先用ExecuteScalar来判断,如果>0就不加入 --------------------编程问答-------------------- 写个存储过程,先select where Stationname=1391111111 判断存在就插入 --------------------编程问答-------------------- 你是要更新还是插入啊。 --------------------编程问答-------------------- 直接加入就行 --------------------编程问答-------------------- if exists(select 1 from table where Stationname=1391111111)
begin
insert into .........
end --------------------编程问答-------------------- insert into ...select..from ....where  这种可以加条件
insert into ....values()   这种不行  --------------------编程问答-------------------- 没看出来你的 Where条件 和insert语句有什么关系 

如果 Stationname 是 PlanMssion_ShangChuan 这张表的字段 
    那么 where Stationname=1391111111 完全用不着 
    因为要新增的数据还不在数据库中 就算你能where 也木有效果

如果 Stationname 不是 PlanMssion_ShangChuan 这张表的字段 
    那 where Stationname=1391111111 跟Insert语句 就更加木有关系了
    直接在sql语句外面做判断就可以了

注: insert语句是不能where的 where一般是查询用的
--------------------编程问答-------------------- 好像这样不行吧,加上WHERE好像是更新的吧
--------------------编程问答-------------------- 先 select  × where Stationname=1391111111 先查找出来,存在再执行 插入语句 --------------------编程问答-------------------- declare @cc int
select  @cc=count(*)  from 表 Stationname=1391111111
if @cc>0
  begin
    insert xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  end
else
  begin
   --随便你
  end --------------------编程问答--------------------
引用 11 楼 andyalex 的回复:
先 select × where Stationname=1391111111 先查找出来,存在再执行 插入语句

晕,不会吧,那有根据条件插入记录的呀
都是根据条件更新的记录的呀 --------------------编程问答--------------------
insert into PlanMssion_ShangChuan(Station,JHour,JMinite,MissionParm1,MissionParm2,PressValue,YulvValue) 

select 字段名和insert字段一一对应 from 表 where Stationname=1391111111 --------------------编程问答-------------------- 主要不知道楼主想实现什么,说的很模糊啊. --------------------编程问答-------------------- 先判断一下,不能直接拼SQL上 --------------------编程问答-------------------- 直接sql 拼上
--------------------编程问答-------------------- 建议用"Check约束". --------------------编程问答--------------------
引用楼主 woaifeixiong 的回复:
我在C#里写了一条插入语句,想加上where Stationname=1391111111。如何加呢。必须要满足这个条件后才能插入。
sql += "insert into PlanMssion_ShangChuan(Station,JHour,JMinite,MissionParm1,MissionParm2,PressValue,YulvValue) values('" + ws.Dtuid……


发到 sql 区。
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,