怎么写?我是通过一个textBox输入数据,个button1执行添加。怎样完成向数据库插入数据时,若有重复,则提示“已存在”。

解决方案 »

  1.   

    select count(1) from [table] where condition
    返回值大于0 说明存在
      

  2.   

    select count(*) from table where A=textbox的值 
    如果数据库有这条数据 返回值大于0 判断一下 然后 提示messagebox.show("已存在");
      

  3.   

    select count(*)..判断是否存在
    或not 
    exists(select 1 from table where id=@id) insert into...
      

  4.   

    if not exists(select 1 from table where id=@id) insert into...
      

  5.   

    在数据库里写一个存储过程,类似楼上写的那样
    CREATE PROCEDURE sp_SaveData (
    @Desc nvarchar(260),
    @Result int OUTPUT
    ) ASbegin
    set nocount on -- Result return Error Codes:
    --  0 - Successful.
    --  1 - alreay exist in database. if not exists (select * from 【表】 where 【column名】 = @Desc )
    begin
                  insert into 【表】 (【column名】) values (@Desc )
    set @Result = 0
    return
    end
            else
            begin
                 set @Result = 1
                 return
            end然后在程序中调用这个存储过程并根据返回值 如果返回值result为1表示已经有记录
      

  6.   

       string sqlMold_pn = "select 1 from mold_base where mold_pn='" + tnMold_Pn.Text + "'";
    判断是否大於1