当前位置:首页 » 多媒体相关

主键重复问题???


我在sql数据库中  
  在vb中用datagrid绑定到记录集  
  在用addnew....update增加时,输入重复记录,报错记录重复。  
  此时在datagrid中已增加了这一条重复记录。  
  由于重复了,所以改了主键字段值再存(addnew...update)  
  时出错。此时退出整个程序了。  
  请问有什么办法在数据库判断重复之后,不退出程序下可以再存。  
 

推荐阅读

  • 只看热门的 手持车载GPS导航仪推荐GARMIN 任我游 610GPS [详细内容]
  • 【魔兽战术】不死面对暗夜基本技巧 [详细内容]
  • 隐藏游戏 片片 小说等等的几个方法 [详细内容]
  • 敢问路在何方 新科GPS推出多款产品车载GPS导航设备 [详细内容]
  • 保护(IIS)web服务器的15个技巧 [详细内容]
  • E770的GPRS和彩信设置 [详细内容]
  • 秋天GPS多姿多彩 时尚运动人士至爱手持GPS导航设备 [详细内容]
  • 网友回答:
    网友:lihonggen0

    一般主键设计成自增长的  
       
      在datagrid上不上用户编辑  
       
       
      这样增加一条记录,它自已就生成序号了,用户都不用管  
       
      而且根本不会出现重复记录  
       
     

    网友:lwm1977

    主键重复说明你的库结构不合理

    网友:yijiansong

    捕捉数据库返回错误信息,判断后分别处理

    网友:ch21st

    用事务处理怎么样

    网友:haipingma

    public   function   chksame(byval   adors   as   adodb.recordset,   byval   strfld   as   string   )   as   boolean  
                    chksame   =   false  
                    dim   clors   as   new   adodb.recordset  
                    if   adors.recordcount   >   0   then  
                          adors.movefirst  
                          do   while   not   adors.eof  
                                set   clors   =   adors.clone  
                                clors.filter   =   "序號<>"   &   adors.fields("序號").value  
                                if   clors.recordcount   <=   0   then   exit   function  
                                      clors.movefirst  
                                      clors.find   strfld   &   "="   &   adors.fields(strfld).value   &   ""  
                                      if   not   clors.eof   then  
                                          chksame   =   true  
                                          exit   function  
                                    end   if  
                                    set   clors   =   nothing  
                                    adors.movenext  
                          loop  
                    end   if  
      end   function  
      序號為自動增加的欄位

    .

    讨论区

    Login