procedure ShowADOErrors( con: TADOConnection); var i: Integer; strErrorList: TStrings; begin if not con.Errors.Count > 0 then Exit; strErrorList := TStringList.Create; try for i := 0 to con.Errors.Count - 1 do begin strErrorList.Add(con.Errors.Item[i].Source + ' : ' + con.Errors.Item[i].Description + ' ; ' + con.Errors.Item[i].SQLState); end; if trim(strErrorList.Text) = '' then Exit; MessageDlg(strErrorList.Text, mtWarning, [mbOK], 0); finally strErrorList.Free; end; end; //*************************************** 使用: try except showadoerror(con); end
使用 try except 啊 还有 try finally
在PostError事件中 procedure ShowADOErrors( con: TADOConnection); var i: Integer; strErrorList: TStrings; begin if not con.Errors.Count > 0 then Exit; strErrorList := TStringList.Create; try for i := 0 to con.Errors.Count - 1 do begin strErrorList.Add(con.Errors.Item[i].Source + ' : ' + con.Errors.Item[i].Description + ' ; ' + con.Errors.Item[i].SQLState); end; if trim(strErrorList.Text) = '' then Exit; MessageDlg(strErrorList.Text, mtWarning, [mbOK], 0); finally strErrorList.Free; end; end; //*************************************** 使用: try except showadoerror(con); end
有个api :getlasterror,可以试试,不过仅限win32系统
不行,我这样写: procedure TForm1.Button1Click(Sender: TObject); var i: Integer; strErrorList: TStrings; begin try with adoquery1 do begin sql.Clear; sql.Add('insert into yt (ytname) values('); sql.Add('''yt'''+')'); adoquery1.ExecSQL; end; except begin listbox1.Items.Add(adoconnection1.Errors.Item[0].Description); listbox1.Items.Add(adoconnection1.Errors.Item[1].Source); listbox1.Items.Add(adoconnection1.Errors.Item[2].SQLState); listbox1.Items.Add(adoconnection1.Errors.Item[3].HelpFile); end; end; end; 在listbox中显示: 违反了primary key约束'pk_yt'.不能在对象'yt'中插入重复键。 Microsoft OLE DB Provider for SQL Server 但会弹出窗口: 项目在所需的名称或序数中未被发现 请问这个信息怎么捕捉屏蔽?
con: TADOConnection);
var
i: Integer;
strErrorList: TStrings;
begin
if not con.Errors.Count > 0 then
Exit; strErrorList := TStringList.Create;
try
for i := 0 to con.Errors.Count - 1 do
begin
strErrorList.Add(con.Errors.Item[i].Source + ' : ' +
con.Errors.Item[i].Description + ' ; ' +
con.Errors.Item[i].SQLState);
end;
if trim(strErrorList.Text) = '' then Exit;
MessageDlg(strErrorList.Text, mtWarning, [mbOK], 0);
finally
strErrorList.Free;
end;
end;
//***************************************
使用:
try
except
showadoerror(con);
end
使用 try except 啊
还有 try finally
procedure ShowADOErrors(
con: TADOConnection);
var
i: Integer;
strErrorList: TStrings;
begin
if not con.Errors.Count > 0 then
Exit; strErrorList := TStringList.Create;
try
for i := 0 to con.Errors.Count - 1 do
begin
strErrorList.Add(con.Errors.Item[i].Source + ' : ' +
con.Errors.Item[i].Description + ' ; ' +
con.Errors.Item[i].SQLState);
end;
if trim(strErrorList.Text) = '' then Exit;
MessageDlg(strErrorList.Text, mtWarning, [mbOK], 0);
finally
strErrorList.Free;
end;
end;
//***************************************
使用:
try
except
showadoerror(con);
end
procedure TForm1.Button1Click(Sender: TObject);
var
i: Integer;
strErrorList: TStrings;
begin try
with adoquery1 do
begin
sql.Clear;
sql.Add('insert into yt (ytname) values(');
sql.Add('''yt'''+')');
adoquery1.ExecSQL;
end;
except
begin
listbox1.Items.Add(adoconnection1.Errors.Item[0].Description);
listbox1.Items.Add(adoconnection1.Errors.Item[1].Source);
listbox1.Items.Add(adoconnection1.Errors.Item[2].SQLState);
listbox1.Items.Add(adoconnection1.Errors.Item[3].HelpFile);
end;
end;
end;
在listbox中显示:
违反了primary key约束'pk_yt'.不能在对象'yt'中插入重复键。
Microsoft OLE DB Provider for SQL Server
但会弹出窗口:
项目在所需的名称或序数中未被发现
请问这个信息怎么捕捉屏蔽?