ADOConnection 连接异常问题?

airhand 2002-11-29 04:59:43
try
adoconnection.connectstring:='......';
adoconnection.open(usrid,passwd);
adoconnection.close;
showmessage('连接成功');
exception
on eoleexcept do
begin
adoconnection.close;
adoconnection.free;
end;
on eaccessexcept do
begin
adoconnection.close;
adoconnection.free;
end;
end;

但运行时如果连接串错误或密码错误就会先弹出系统的eoleexcept和eaccessexcept异常对话框,而不执行我定义的异常处理过程,如果继续点击运行,才弹出我写的提示。请问如何屏蔽前面的异常提示,直接由我的异常处理接管?
...全文
135 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
dawangzi16 2003-04-21
  • 打赏
  • 举报
回复
All are right!If you execute a executable file ,the problem will be solved!
zhoutian618 2003-04-21
  • 打赏
  • 举报
回复
直接运行EXE试试看!

如果还不行就去截Application.OnException事件一定可以解决!
gzyzljk 2003-04-21
  • 打赏
  • 举报
回复
直接执行Exe文件
tobelost 2003-04-21
  • 打赏
  • 举报
回复
Tools -> Debug Options -> Language Exceptions

do NOT check the "Stop on Delphi exceptions"

shadou 2003-03-14
  • 打赏
  • 举报
回复
你可以设置断点试试

在open的前面一句设置,应该是open时报错吧!

delphi里直接运行时,有错会先报出来的,然后才执行你的异常操作的
shadou 2003-03-14
  • 打赏
  • 举报
回复
try
adoconnection.close;//这样试试
adoconnection.connectstring:='......';
adoconnection.open(usrid,passwd);
adoconnection.close;
showmessage('连接成功');
exception
on eoleexcept do
begin
adoconnection.close;
adoconnection.free;
end;
on eaccessexcept do
begin
adoconnection.close;
adoconnection.free;
end;
end;
wpr_tzp 2003-03-14
  • 打赏
  • 举报
回复
你是在D里直接运行时才出这样的吧,你直接执行Exe文件就不会先弹出DEPHI的异常提示,而是直接执行你的异常处理了
airhand 2002-12-02
  • 打赏
  • 举报
回复
楼上请看清问题,我说的是为什么会先弹出DEPHI的异常提示,不直接到我的异常处理。如何写异常我还是知道的,不要答地不着边际!
hamzsy 2002-11-30
  • 打赏
  • 举报
回复
try

adoconnection.connectstring:='......';
adoconnection.open(usrid,passwd);
adoconnection.close;
showmessage('连接成功');

exception(这里定义错误所属的类型 比如:Exception err)

MessageBox.Show(err.message);

end;
airhand 2002-11-29
  • 打赏
  • 举报
回复
楼上,我问的是DEPHI中如何解决,不是VC或JAVA!!!
visualcpu 2002-11-29
  • 打赏
  • 举报
回复
try
{
adoconnection.connectstring:='......';
adoconnection.open(usrid,passwd);
adoconnection.close;
showmessage('连接成功');
}
catch(这里定义错误所属的类型 比如:Exception err)
{
MessageBox.Show(err.message);
}
一个使用ADO连接池的示例,演示了TADOStoredProc动态参数的使用,带重连机制 =================== unit UnitDemo; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm2 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); procedure FormCreate(Sender: TObject); procedure FormDestroy(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form2: TForm2; //数据库服务器 gDBServer: String = '127.0.0.1'; //数据库名称 gDBName: String = 'master'; //数据库用户名 gDBUser: String = 'sa'; //密码 gDBPass: String = '2001'; implementation {$R *.dfm} uses ADODB, UnitADOConnectionPool; const CreateSQL = 'create procedure TestMyPool (@type sysname) '#13#10+ 'as'#13#10+ 'select * from sysobjects where xtype=@type'#13#10+ 'return @@rowcount'; DeleteSQL = 'if Exists(select 1 from sysobjects where xtype=N''P'' and name=N''TestMyPool'')'#13#10+ ' drop procedure TestMyPool'; var gPoolMan: TADOConnPoolMan = Nil; procedure TForm2.Button1Click(Sender: TObject); var ADOObject:TADOConnPoolObject; ADOStoredProc:TADOStoredProc; Running :Integer; I: Integer; begin //取得一个存储过程资源(含一数据库有效连接ADOObject := gPoolMan.CreateSP('TestMyPool'); if ADOObject = Nil then //取得资源失败 Exit; try ADOStoredProc := ADOObject.ExecObject as TADOStoredProc; Running := 2;//允许重试(两次)操作,以便在操作失败之后达到重连 while Running>0 do begin Dec(Running); if ADOObject.NeedRefresh then begin//判断是否有重连标志(比如数据库断开等,可能需要进行重连) if Not ADOObject.Reconnect then Exit; ADOObject.NeedRefresh := Not ADOStoredProc.Parameters.Refresh; if ADOObject.NeedRefresh then Exit; end; for I := 1(*Zero is the *Result* Parameter*) to ADOStoredProc.Parameters.Count - 1 do begin //========================= //传递参数 ADOStoredProc.Parameters.Items[I].Value := 'U'; //========================= end; if Running 0 then try //执行存储过程 ADOStoredProc.Open; //执行存储过程成功,退出循环进入后续的数据处理 break; except On E:Exception do begin //执行失败非程序级的异常通常有两种可能: //1.数据库连接断开 //2.自适合的参数传递当中可能存储过程已更新,参与不一致 //设置重连标志 ADOObject.NeedRefresh := True; //=================== //这里记录数据库操作失败日志 //=================== end; end; Exit; end; //========================== //从ADOStoredProc当中读取记录 ShowMessage(IntToStr(ADOStoredProc.Parameters.ParamByName('Result').Value)); //========================== //关闭存储对象的资源 ADOStoredProc.Close; finally //调用结束,释放资源 ADOObject.Free; end; end; procedure TForm2.FormCreate(Sender: TObject); var ADOConn:TADOConnection; begin (****************BEGIN*******************) (*注:仅为测试准备 *) //初始化测试环境 ADOConn := Nil; if Not TADOConnPoolMan.ConnectADO( gDBServer,gDBUser,gDBPass,gDBName,true,ADOConn) then Exit; try ADOConn.Execute(DeleteSQL); ADOConn.Execute(CreateSQL); finally try ADOConn.Close; except end; ADOConn.Free; end; (*****************END********************) //初始化连接池 gPoolMan := TADOConnPoolMan.Create(gDBServer,gDBUser,gDBPass,gDBName,true); end; procedure TForm2.FormDestroy(Sender: TObject); var ADOConn:TADOConnection; begin //释放连接池 if Assigned(gPoolMan) then gPoolMan.Free; (****************BEGIN*******************) (*注:仅为测试准备 *) //清理测试环境 ADOConn := Nil; if Not TADOConnPoolMan.ConnectADO( gDBServer,gDBUser,gDBPass,gDBName,true,ADOConn) then Exit; try ADOConn.Execute(DeleteSQL); finally try ADOConn.Close; except end; ADOConn.Free; end; (*****************END********************) end; end.

2,507

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 数据库相关
社区管理员
  • 数据库相关社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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