$200 Wanted it!谁能给个多线程处理数据库的源代码!或请教Delphi多线程同步处理多个存储过程?

leon2000 2000-08-28 10:03:00
1.我在使用多线程时出现错误:raise execption class EnoResultSet with message
'Error creating cursor handle'
2.线程代码为

unit spThreadUnit;

interface

uses

Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Menus, StdCtrls, Buttons, ScktComp, ExtCtrls, ComCtrls, Db, DBTables,inifiles;

type
spThread = class(TThread)
private
{ Private declarations }
Asp:TStoredProc;
Askt:TCustomWinSocket;

protected
procedure Execute; override;
procedure DisLoop;
public
constructor Create(sp:TStoredProc;skt:TCustomWinSocket);
end;

implementation

{ Important: Methods and properties of objects in VCL can only be used in a
method called using Synchronize, for example,

Synchronize(UpdateCaption);

and UpdateCaption could look like,

procedure myThread.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end; }

{ myThread }
constructor spThread.Create(sp:TstoredProc;skt:TCustomWinSocket);
begin
inherited Create(False);
Asp:=sp;
Askt :=skt;
FreeonTerminate:=True;
end;

procedure spThread.DisLoop;
begin
// Aedit.text:=InttoStr(CurrentLoop);
with Asp do
begin
close;
open;
while not eof do
begin
Askt.SendText(fields[0].AsString+#13+#10);
next;
end;
//I am sure there is a record to return
end;
end;

procedure spThread.Execute;
var i:integer;
begin
{ Place thread code here }
synchronize(disLoop);
if Terminated then exit;
end;

end.

3. 是否要使用Tsession?

...全文
287 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
dragongong 2000-09-14
  • 打赏
  • 举报
回复
It's my pleasure! Keeping touch.
leon2000 2000-09-13
  • 打赏
  • 举报
回复
dragongong,you have been very helpful!
I want to give you 200,but it dose not work!
Maybe I am a jounor user of this web!
I only can give you 107 which is full mark of this question!
Thanks again!
leon2000 2000-09-13
  • 打赏
  • 举报
回复
Thanks for your kind help!
dragongong 2000-09-12
  • 打赏
  • 举报
回复
sorry, i'm very busy recently. now give you a quite simply sample .

unit threadunit;

interface

uses classes, stdctrls, sysutils, dbtables, forms;

type
TSampleThread = class(TThread)
private
{ Private declarations }
FSP:TStoredProc;
FMemo:TMemo;
FSession:TSession;
FThreadDBException: Exception;
protected
procedure Execute; override;
procedure show;
procedure showError;
public
constructor Create(sp:TStoredProc;M:TMemo);
end;


implementation

{ TSampleThread }

constructor TSampleThread.Create(sp: TStoredProc; M: TMemo);
begin
inherited create(true);
Fsession:= TSession.create(Application);
FSP:= sp;
FMemo:= M;
FreeOnTerminate := True;
resume;
end;

procedure TSampleThread.Execute;
begin
try
FSP.open;
show; // here needn't use synchronize because synchronize procedure is run in the VCL main thread.
except
FThreadDBException := Exceptobject as Exception;
synchronize(showerror);
end;
end;

procedure TSampleThread.show;
begin
Fsp.first;
while not Fsp.eof do
begin
FMemo.Lines.Add(FSP.Fields[0].asstring);
Fsp.next;
end;
end;

procedure TSampleThread.showError;
begin
Application.showexception(FThreadDBException);
end;

end.
dragongong 2000-09-12
  • 打赏
  • 举报
回复
sorry, I'm so busy recently. now give you a quite simple sample. enjoy!

unit threadunit;

interface

uses classes, stdctrls, sysutils, dbtables, forms;

type
TSampleThread = class(TThread)
private
{ Private declarations }
FSP:TStoredProc;
FMemo:TMemo;
FSession:TSession;
FThreadDBException: Exception;
protected
procedure Execute; override;
procedure show;
procedure showError;
public
constructor Create(sp:TStoredProc;M:TMemo);
end;


implementation

{ TSampleThread }

constructor TSampleThread.Create(sp: TStoredProc; M: TMemo);
begin
inherited create(true);
Fsession:= TSession.create(Application);
FSP:= sp;
FMemo:= M;
FreeOnTerminate := True;
resume;
end;

procedure TSampleThread.Execute;
begin
try
FSP.open;
show; // here needn't use synchronize because synchronize procedure is run in the VCL main thread.
except
FThreadDBException := Exceptobject as Exception;
synchronize(show);
end;
end;

procedure TSampleThread.show;
begin
Fsp.first;
while not Fsp.eof do
begin
FMemo.Lines.Add(FSP.Fields[0].asstring);
Fsp.next;
end;
end;

procedure TSampleThread.showError;
begin
Application.showexception(FThreadDBException);
end;

end.
leon2000 2000-09-10
  • 打赏
  • 举报
回复
dragongong,Thanks for your help!
But I can be sure there are some records rerurned!
when I use the single thread,It run properly!
So,Please give a simple sample for using multi thread to deal with database
storedProc!
dragongong 2000-09-06
  • 打赏
  • 举报
回复
TSampleThread = class(TThread)
TSampleThread.Create(Session:TSession; ASP:TStoreProc;Askt:TCustomWinSocket);
begin
inherited create(false);
ASP.sessionname:= session.sessionname;
....
end;
另外,你的错误与线程无关,是你激活ASP:TStoreProc使用的是open, 而StoreProc可能有错误没有返回值,所以才会报错:'Error creating cursor handle'. 我以前碰到类似问题,你最好再仔细检查一下
qiuzhi 2000-09-05
  • 打赏
  • 举报
回复
关注
leon2000 2000-09-05
  • 打赏
  • 举报
回复
谁能给个简单的例子吗?我一定将200分献上!
-如何使用Tsession,在线程代码里为每一个存储过程指定呢?还是在父程序中?
能给个简单的例子吗?
leon2000 2000-08-28
  • 打赏
  • 举报
回复
keboy,thank you!
但是如何使用Tsession,在线程代码里为每一个存储过程指定呢?还是在父程序中?
能给个简单的例子吗?我一定将200分献上!
keboy 2000-08-28
  • 打赏
  • 举报
回复
在多线程处理数据库时,肯定要用到TSession,
不同的SessionName代表多线程,所以AutoSessionName要设为False;
再自己写一个区别Session
keboy 2000-08-28
  • 打赏
  • 举报
回复
在多线程处理数据库时,肯定要用到TSession,
不同的SessionName代表多线程,所以AutoSessionName要设为False;
再自己写一个区别SessionName的函数进行调用;

2,496

社区成员

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

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