if(GetCount(Sql)>0)
{
  fun.setalert("warning!");
}

解决方案 »

  1.   

    OleDbCommand cmd = new OleDbCommand("select * from tb where fd ='abc'",Conn);
    OleDbDataReader Rd = Cmd.ExecuteReader();
    if (Rd.Read())
    {
        Resposne.Write("数据库中已存在");
    }
      

  2.   

    在按保存的按扭里加一个判断,我的是这样的,定义一个公共的方法:
    如IsUnique,condition是条件,如empno='kkk'
    public bool(string tablename,string condition)
    mselect ="select count(*) from '"+tableName+"' where '"+condition +"'";
    OleDbCommand cmd = new OleDbCommand(mselect,Conn);
    int k = System.Convert.Toint16(cmd.ExecuteScalar());
    if(k>0)
    {
    有重复记录
    }
      

  3.   

    下面是一个简单例子:2627  主索引重复
    2601  唯一索引重复try
    {
    }
    catch(SqlException e)
    {
    switch(e.Number)
    {
    case 2627:
    //Message "Violation of PRIMARY KEY constraint 'User_Main_PK'. 
    // Cannot insert duplicate key in object 'User_Main'.
    // 主索引重复
    throw new Exception("你注册的用户已经存在,请换一个用户名重新注册!!");
    case 2601:
    // 唯一索引约束违反
    throw new Exception("你注册用的Email已经存在,请换一个Email重新注册!!");
    default:
    throw e;
    }
    }
    }
      

  4.   

    OleDbCommand cmd = new OleDbCommand("select * from tb where fd ='abc'",Conn);
    OleDbDataReader Rd = Cmd.ExecuteReader();