判断SQL数据库是否存在表,是否存在记录

SQL数据库,当判断一条记录存在时,则更新更记录,当记录不存在时,则添加该记录

使用SQL语句在C#中实现,sql语句

if exists 
(select * from 表 where 条件)
 begin
 Update 表  Set 字段=字段值 where 条件
 end
 else
 begin
 Insert into dbo.表名(字段)  values
 (字段值)
end



比如:

if exists 
(select * from CollectRain_201404 where StationId='D6021' and CollectTime='20140406232000')
 begin
 Update CollectRain_201404  Set CollectRainFall='22222',IsValue='false'  where StationId='D6021' and CollectTime='20140406232000'
 end
 else
 begin
 Insert into dbo.CollectRain_201404 (StationId,CollectTime,CollectRainFall,IsValue)  values
 ('D6021','20140406232000','11111','true')
end


当判断一个表是否存在数据库时,当数据库不存在该表时,则需要新建该表

使用SQL在C# NET中实现方法

if exists 
(select * from sysobjects where id = object_id(N'[表名]') 
 and OBJECTPROPERTY(id, N'IsUserTable') = 1) 
 print 'f'
 else
 begin
 CREATE TABLE 表名
(
建表
)
end

比如

if exists 
(select * from sysobjects where id = object_id(N'[Test]') 
 and OBJECTPROPERTY(id, N'IsUserTable') = 1) 
 print 'f'
 else
 begin
 CREATE TABLE Test
(
Id int IDENTITY(1,1) PRIMARY KEY,
StationId nvarchar(MAX) NOT NULL,
CollectTime nvarchar(MAX) not null,
CollectRainFall float not null,
IsValue bit not null
)
end

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值