数据录入问题!(70分)

C

camcap

Unregistered / Unconfirmed
GUEST, unregistred user!
请问:我要限制客户的输入规范,比如在一些表里面的主键字段,这个字段里面的记录肯定
是唯一的.客户可能不小心重复输入了相同的记录,这时候系统肯定会有错误提示.
请问我想做一个判断程序,如果用户输入相同记录,我就弹出出错对话框,提示该记
录存在,不用重复输入了.请问该怎么写.烦请代码指点一下.谢谢^_^
 
保存之前在数据库查找有没有此单号,如有则错,没有就OK!
 
用try
...
except
...
end;
捕捉错误。
 
http://www.delphibbs.com/delphibbs/DispQ.asp?LID=1724593
 
这是我们的一段程序,大体都一样
query1.close;
query1.SQL.clear;
ss := ' select count(*) from zsjk1';
query1.sql.add(ss);
ss := ' where mbmc=''' + ss1 + '''';
query1.sql.add(ss);
query1.open;
if query1.Fields[0].asinteger > 0 then
begin
showmessage('模板名重复');
query1.close;
query1.SQL.clear;
exit;
end;
 
query1.close;
query1.SQL.clear;
query1.sql.add('select count(*) as num from 表名 where 字段名= '''+edit1.text+'''');
query1.open;
if query1.fieldbyname('num').asinteger>0 then
begin
showmessage('记录重复!');
query1.close;
edit1.setfocus;
exit;
end
else
begin
query1.close;
{在这里正常写入}
end;
 
用try语句
 
这个问题对于搞数据库开发的程序员应该是经常碰到的
我不知道你的表名和关键字是什么
所以在这里只好自己定义变量了
然后你再改过来好了
假如你的那个字段由edit1来输入;
var
biao:string;//表名
ziduan:string;//该表的关键字
begin
query1.close;
query1.sql.clear;
query1.sql.add('select * from biao where ziduan='''+eidt1.text+'''');
query1.open;
if query1.recordcount>0 then
begin
showmessage('数据库中已经存在这条记录!');
edit1.selectall;
exit;
end;
query2.post;
end;
注意:这里的query1是用来查询该数据库中是不是有该条记录的,query2是你用来将数据提交数据库的
他两个不能用同一个代替,那样会冲突的。

不知道这样的答案你是否满意
如果满意请给分吧
 
with query1 do
begin
try
ApplyUpdates;
CommitUpdates;
execption
on E: exception do
begin
if pos('PRIMARY KEY', E.message) <> 0 then
begin
Application.MessageBox('出现关键字冲突错误,请修改关键字值。', '出错信息',
MB_Ok or MB_ICONWARNING);
end;
cancelupdates;
end;
end;
 
同意tophi,我有試過,ok
 
有沒有搞錯!

這樣的問題你們竟然這樣寫,還是不是搞數據庫存的?
close;
sql.clear;
sql.add('select * from table where id='+dbedit1.text);
prepared;
open
if not adoquery.eof then
showmessage("有相同ID號");
exit
 
我一般用wyc_ado的方法,还不错
 
用locate是不是简单一些
 
楼主去这里看看吧:
http://delphibbs.com/delphibbs/dispq.asp?lid=2285820
应该解决了,当然方法不只这些
 
function checkid(id:string):boolean;
var
adoquery1:Tadoquery;
begin
result:=false;
adoquery1:=tadoquery.create(nil);
try
adoquery1.Connection:=...;
with adoquery1 then
begin
close;
sql.clear;
sql.add('select * from table where id='+id);
prepared;
open;
end;
if adoquery1.isempty then
begin
result:=true;
end;
finally
adoquery1.free;
end;
end;
 
有没有其他好一点的方法了
 
想確定記錄是否存在那就找一下不就可以了嗎?
 
让系统自动编码!不让客户手动,又快又好!呵呵。
 
在用户输入完以后,当输入的焦点装移之前作校验,用sql查询是否存在于主键中

我以前做的就使用这中方法
 
顶部