在绑定情况下输入新记录怎样来判断主键值重复?

dqiuqiu 2001-08-14 05:26:57
如:表Table1中有MyNo字段为主键,我用DATA CONTROLS中的控件输入记录,当在点下〈保存〉按钮时(调用POST方法),如Myno字段有重复值时会出错。想请教一下怎样才能判断有重复值而不进行保存。用什么方法或事件解决?能给出代码示例最好。(一定得用绑定)。
...全文
207 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
gwbasic 2001-08-15
  • 打赏
  • 举报
回复
绑定会更容易点,
以前用非绑定,我现在改用绑定
zhhyoushen 2001-08-15
  • 打赏
  • 举报
回复
用locate吧,看一下,我想会有帮助的
function Locate(const KeyFields: string; const KeyValues: Variant; Options: TLocateOptions): Boolean; virtual;

Description

This function returns False, indicating that a matching record was not found and the cursor is not repositioned.

Descendant classes override this method so that it locates the record where the fields identified by the comma-delimited string KeyFields have the values specified by the Variant or Variant array KeyValues. Options indicates whether the search is case insensitive and whether partial matches are supported. Locate should return True if a record is found that matches the specified criteria and the cursor repositioned to that record.
dqiuqiu 2001-08-15
  • 打赏
  • 举报
回复
请大家再帮帮我吧·!
dqiuqiu 2001-08-14
  • 打赏
  • 举报
回复
顺便问一下,不知大家在做单记录输入的表单时(字段很多),是用控件绑定还是非绑定呢?
dqiuqiu 2001-08-14
  • 打赏
  • 举报
回复
谢谢各位参与。问题就在于因为是绑定,用locate方法的时候,记录指针一移动,数据库里已添加了这笔记录,如果是主键重复,就会出现错误。所以得用什么办法来解决?
wzs_wzs123 2001-08-14
  • 打赏
  • 举报
回复
我同意楼上的,先上数据库中查一下,看有没有输入的值.
gwbasic 2001-08-14
  • 打赏
  • 举报
回复
我也没好办法,只在保存前用
Query 查一下是有此记录,如有,则给出提示信息

With ADOQuery do
begin
if Active then Close;
SQL.Clear;
SQL.Add('select MyNo from MyTable where MyNo=' + IntToStr(MyNo));
Open;
If not Eof then
begin
Close;
ShowMessage('主键重复');
Abort;
end;
Close;
end;
yexiaofeng 2001-08-14
  • 打赏
  • 举报
回复
使用tabel.LOCATE
具体用法看delphi帮助

5,392

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧