"Key Violation" with ClientDataSet

探讨了在数据库自动编号为主键的情况下,如何在客户端进行数据验证的问题,提供了两种解决方案:一是修改数据提供者(tdatasetprovider)源代码,二是手动设置uniquery的keyfields字段。

产生问题的根源是Provider会自动获取数据库中的primerkey,到前台ClientDataSet中做验证。

有的时候我们并不需要再前台验证,或者当SQL过于复杂的时候,它并不能正确产生primerkey的验证逻辑。

修改方式有二:

1.这个问题在于datasetprovider (tdatasetprovider)在中间层打包数据时,把数据约束(例如 主键)的信息也打包进去,然后到了客户端,clientdataset (tclientdataset)就会自动判断这些约束条件。但是在数据库设置的自动增量,是数据库自动生成的,这个时候如果在客户端添加数据,自动编号始终为空,就出问题了,因为这个时候数据根本没有提交给数据库,无法自动生成自动编号,可能解决的办法就是改datasetprovider (tdatasetprovider)的源代码,当遇到是自动编号做主键的时候,就不把主键信息打包到数据包里,也没有必要打包到数据包里,因为这个主键由数据库保障了。

先打开Provider.pas,然后另存为到工程目录下面,然后找到procedure TDataPacketWriter.AddIndexDefs(DataSet: TDataSet; const Info: TInfoArray);这个过程,把          if not VarIsNull(KeyList) then
          AddAttribute(pcktAttrArea, szPRIMARY_KEY, KeyList, False);
这两句代码屏蔽掉就可以了

 

2.手动设置uniquery的keyfields字段,如:COMP_CODE;EMP_CODE;EQU_COURSECODE

Delphi 7.1 Update Release Notes=======================================================This file contains important supplemental and late-breakinginformation that may not appear in the main productdocumentation, and supersedes information contained in otherdocuments, including previously installed release notes.Borland recommends that you read this file in its entirety.NOTE: If you are updating a localized version of Delphi 7, visit the Borland Registered User web site to obtain a localized readme file that may contain important late- breaking information not included in this readme file.IMPORTANT: Delphi must be closed before installing this update. =====================================================CONTENTS * INSTALLING THIS UPDATE * UPDATING LOCALIZED VERSIONS OF DELPHI 7 * KNOWN ISSUES * ISSUES ADDRESSED BY THIS UPDATE - IDE - CORE DATABASE - DATASNAP - DBGO (ADO COMPONENTS) - dbExpress - dbExpress COMPONENTS AND DB VCL - dbExpress CORE DRIVER AND METADATA - dbExpress VENDOR ISSUES - dbExpress CERTIFICATION - WEB SNAP - ACTIVEX - COMPILER - RTL - VCL - THIRD PARTY - BOLD FOR DELPHI * VERIFYING THAT THE UPDATE WAS SUCCESSFUL * FILES INSTALLED BY THIS UPDATE =======================================================INSTALLING THIS UPDATE* This update can not be applied to Delphi 7 Architect Trial version. * This update can not be removed after it is installed.* You will need the original Delphi 7 installation CD available to install this update.* To install this update from the CD, insert the CD, and launch the d7_ent_upd1.exe file appropriate for your locale.* To install this update from the Web, double-click the self-executing installation file and follow the prompts. * The Delphi 7 documentation PDF files are available on the update CD.========================================================UPDATING LOCALIZED VERSIONS OF DELPHI 7* This update can be applied only to the English version of Delphi 7. There are separate updates for the German, French and Japanese ver
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值