//需要使用EOleException,需要引用ComObj单元,
//只是简单测试,也不敢肯定没错误
procedure TForm1.Button1Click(Sender: TObject);
var
s:string;
begin
with qry do
begin
if not connection.Connected then
connection.Connected:=true;
close;
s:='insert into tbDemo(uno,fname)values(' + trim(Edit1.Text) + ',''' + trim(Edit2.Text) + ''')';
SQL.Clear;
SQL.Add(s);
try
ExecSQL;
except on e:EOleException do
application.MessageBox(pchar(inttostr(e.ErrorCode)),'系统提示');
//ErrorCode=-2147217873 利用此值来判断是主键重复了,直接告知操作的用户,这样的效率较高
//经检测每次都返回该值,感觉微软的ADO还没有Borland的ODAC做得好
end;//end try
end;//end with
end;