function TFormDep.dep_node_exist(id: string): integer; var strSQL : string; begin with DataModuleManage.ADOQueryPub1 do begin Close; SQL.Clear; Parameters.Clear; Parameters.AddParameter.Name := 'p1'; Parameters.ParamValues['p1'] := id; strSQL := 'select count(*) as a from dep_info where dep_id =:p1'; SQL.Add(strSQL); Open; result := FieldByName('a').AsInteger; end; end; if dep_node_exist(EditDepID.Text) = 0 then begin strSQL := 'insert into dep_info values(:p1,:p2,:p3,:p4)'; TreeViewInfo.Items.AddChild(cur, EditDepName.Text + '—' + EditDepID.Text); end else begin strSQL := 'update dep_info set dep_name=:p2, dep_pic=:p4 where dep_id =:p1'; cur.Text := trim(EditDepName.Text) + '—' +trim(EditDepID.Text); end;
先声明变量: VAR E:Exception;接着:try AdoDataSet.UPdatebatch ; except with E do begin if pos('Key Violation',message)>0 then begin (两种方法) 1、( message:='关键字重复或其他中文信息' raise;) 或 2、(application.messagebox('关键字重复或其他中文信息'),'标题',图标); end; end; end;
var
strSQL : string;
begin
with DataModuleManage.ADOQueryPub1 do
begin
Close;
SQL.Clear;
Parameters.Clear;
Parameters.AddParameter.Name := 'p1';
Parameters.ParamValues['p1'] := id;
strSQL := 'select count(*) as a from dep_info where dep_id =:p1';
SQL.Add(strSQL);
Open;
result := FieldByName('a').AsInteger;
end;
end;
if dep_node_exist(EditDepID.Text) = 0 then
begin
strSQL := 'insert into dep_info values(:p1,:p2,:p3,:p4)';
TreeViewInfo.Items.AddChild(cur, EditDepName.Text + '—' + EditDepID.Text);
end
else
begin
strSQL := 'update dep_info set dep_name=:p2, dep_pic=:p4 where dep_id =:p1';
cur.Text := trim(EditDepName.Text) + '—' +trim(EditDepID.Text);
end;
VAR
E:Exception;接着:try
AdoDataSet.UPdatebatch ;
except
with E do
begin
if pos('Key Violation',message)>0 then
begin
(两种方法)
1、( message:='关键字重复或其他中文信息'
raise;)
或
2、(application.messagebox('关键字重复或其他中文信息'),'标题',图标);
end;
end;
end;