您的位置:程序门 -> delphi -> 数据库相关



关于:查询数据库,长时间等待时的动态提示


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


关于:查询数据库,长时间等待时的动态提示[已结贴,结贴人:13193887977]
发表于:2007-11-17 18:02:39 楼主
查询数据库,长时间等待时的动态提示,我自己做了一个,但有个问题:当执行一段存储过程时,若抛出错误(raiserror),那么在程序中得到的message并不正确。程序代码如下:

----------------------------------------过程代码---------------------------
create   procedure   sp_testproc               --mssql2000
as
begin
    raiserror('其它用户正在进行数据处理,请稍后!',16,1);
end

---------------------------------提示窗口的pas,没有任何代码-------------------
unit   umsgbox;

interface

uses
    windows,   messages,   sysutils,   variants,   classes,   graphics,   controls,   forms,
    dialogs,   extctrls,   comctrls,   gifimage,   stdctrls;

type
    tmsgbox   =   class(tform)
        panel1:   tpanel;
        image1:   timage;
        label1:   tlabel;
    private
        {   private   declarations   }
    public
        {   public   declarations   }
    end;

var
    msgbox:   tmsgbox;

implementation

{$r   *.dfm}

end.
---------------------------------------执行sql的线程pas-----------------------------
unit   umessageboxthread;

interface

uses
    classes,   sysutils,   windows;

type
    tmessageboxthread   =   class(tthread)
    private
        {   private   declarations   }
    protected
        procedure   EXECute;   override;
    public
        proc:pointer;
        errmsg:string;
    end;

implementation

{   important:   methods   and   properties   of   objects   in   visual   components   can   only   be
    used   in   a   method   called   using   synchronize,   for   example,

            synchronize(updatecaption);

    and   updatecaption   could   look   like,

        procedure   updateform.updatecaption;
        begin
            form1.caption   :=   'updated   in   a   thread';
        end;   }

{   updateform   }

procedure   tmessageboxthread.EXECute;
var
    p:procedure;
begin
    {   place   thread   code   here   }
    if   proc <> nil   then   begin
        p:=proc;
        try
            p;
        except
            on   e:exception   do   errmsg:=e.message;
        end;
    end;
    suspend;
end;

end.
-----------------------------------------主程序------------------------------------
//用线程后台执行sql的过程
procedure   showmessagebox(text:string;proc:pointer);
var
    msgthd:tmessageboxthread;
    msgbox:tmsgbox;
    errmsg:string;
begin
    application.mainform.enabled:=false;
    msgbox:=tmsgbox.create(application);
    msgbox.label1.caption:=text;
    msgbox.show;
    msgthd:=tmessageboxthread.create(true);
    msgthd.proc:=proc;
    msgthd.resume;
    while   not   msgthd.suspended   do   begin
        application.processmessages;
        sleep(10);
    end;
    errmsg:=msgthd.errmsg;
    msgthd.terminate;
    msgthd.destroy;
    msgbox.close;
    msgbox.destroy;
    application.mainform.enabled:=true;
    application.bringtofront;
    if   errmsg <> emptystr   then
        raise   exception.create(errmsg);               //// < < <-----------------线程内捕获的异常消息不正确
end;


procedure   u;
begin
    form1.sqlstoredproc1.EXECproc;
end;

procedure   tform1.button1click(sender:   tobject);
begin
    showmessagebox('正在审核[msd0012132]...',@u);
end;

procedure   tform1.button2click(sender:   tobject);
begin
  try
    u;
  except
      on   e:exception   do   showmessage(e.message);                         //// < < <-----------------显示正常
  end;
end;


为什么线程捕获的消息和主进程中捕获的不一样,如果要在查询数据时,显示一个动态的gif或avi,还有其他方法吗?
发表于:2007-11-18 11:40:501楼 得分:0
其实想要的效果是已经实现了,就是发生异常时,捕获的消息不正确。没有人搞过吗?
发表于:2007-11-18 12:31:312楼 得分:0
我觉得线程中的异常和button2click中的异常不是一个异常,
前一个异常好像被改造了
可能是这个原因吧。
顺便问一下,我用一个adoquery组件执行一个时间较长的查询时,
想用一个进度条现实查询的进度,要真的进度条,
显示已查询的记录数怎么实现?
发表于:2007-11-18 18:05:183楼 得分:0
使用adoquery做进度,要将一个异步查询的选项设置好,不是太好做
发表于:2007-12-02 09:06:194楼 得分:0
高手呢?都去哪了?
发表于:2007-12-08 23:39:325楼 得分:0
就是发生异常时,捕获的消息不正确,什么消息是数据库操作的异常消息不正确?
发表于:2007-12-12 16:15:416楼 得分:0
是啊,比如正确的错误消息是:sql   server   error:   sql   state:   42000,   sql   error   code:   5000。
可是捕获的消息却是:dbx   error   .........
发表于:2007-12-12 16:55:367楼 得分:0
up888888888
发表于:2007-12-12 21:35:098楼 得分:20
用线程去查寻比单独进程查询速度慢很多.

不信你查个1万以上的数据看看.呵呵.

我宁愿让用户感觉假死,   也不用线程.
发表于:2008-01-18 11:47:549楼 得分:0
终于找到了答案
http://dn.codegear.com/cn/print/37092


快速检索

最新资讯
热门点击