【问题标题】:checking tha data already exists in the table检查表中已经存在的数据
【发布时间】:2012-11-16 09:14:25
【问题描述】:

我有一个名为brand 的表,字段是:

  • BrandId
  • BrandName

然后我有一个添加品牌的表格。如果我们添加一个品牌名称(例如:Acer)。那么如果我们再次添加相同的品牌,则会检查表中是否已经存在该名称。我如何在 asp.net 中查看它?

【问题讨论】:

  • 表名是指数据库表吗?如果您使用的是哪个db
  • 如何连接到 SQL 服务器?查询或实体框架?
  • 哪个数据库?而且,如果 BrandName 应该是唯一的,那么这个要求应该在数据库表上设置唯一索引为 true 无论您使用什么数据库

标签: c# asp.net


【解决方案1】:

If你想插入新记录而不是使用

if exists(select brandName from [brand] where brandName <> @pbrandName) 
-- insert statement

Else if你想更新现有记录而不是你可以使用..(通过使用现有记录ID)

if exists(select brandName from [brand] where BrandId <> @pBrandId and brandName <> @pbrandName) 
-- update statement

【讨论】:

    【解决方案2】:

    如果你使用的是 Sql 服务器

    declare @Exist INT=0
        Select @Exist= COUNT(BrandName)
        From TableName
        Where BrandId= IdValue
        Group By(BrandName)  
     IF(@Exist=0)
       BEGIN
         --Insert
       END
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-13
      • 2020-02-04
      • 2015-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-10
      相关资源
      最近更新 更多