最近用D7写了一个三层数据库模型,使用了Remote Transactional Datamodule和TClientDataset组件,连接方式是SocketConnection。本来一开始运行的好好的,可是在服务器端程序改动一点点之后,便开始不正常,客户端访问的时候,抛出致命错误,也不知道致命在哪里。下面两段源代码经过比较,没发现有什么特别的改动。
{Code 1: 能够正常运行的代码}
unit uRead;
{$WARN SYMBOL_PLATFORM OFF}
interface
uses
Windows,
{.....}
MidLayer_TLB,
DB,
ADODB,
Provider;
type
TReadFromDatabase = class(TMtsDataModule, IReadFromDatabase)
conRead: TADOConnection;
tblViewEmployees: TADOTable;
cdsEmployees: TClientDataSet;
dspEmployees: TDataSetProvider;
{...}
private
{ Private declarations }
protected
class procedure UpdateRegistry(Register: Boolean; const ClassID, ProgID:
string); override;
procedure PrepareEmployees; safecall;
procedure Login; safecall;
function SignIn(const ARef, APassword: WideString; ATimes: Integer;
var AAdmin, ALogedSuccess, ARejected: WordBool): WideString;
safecall;
public
{ Public declarations }
end;
var
ReadFromDatabase: TReadFromDatabase;
implementation
uses
IniFiles;
{$R *.DFM}
class procedure TReadFromDatabase.UpdateRegistry(Register: Boolean; const
ClassID, ProgID: string);
begin
if Register then
begin
inherited UpdateRegistry(Register, ClassID, ProgID);
EnableSocketTransport(ClassID);
EnableWebTransport(ClassID);
end
else
begin
DisableSocketTransport(ClassID);
DisableWebTransport(ClassID);
inherited UpdateRegistry(Register, ClassID, ProgID);
end;
end;
procedure TReadFromDatabase.PrepareEmployees;
begin
//reserved
end;
procedure TReadFromDatabase.Login;
begin
//reserved
end;
function TReadFromDatabase.SignIn(const ARef, APassword: WideString;
ATimes: Integer; var AAdmin, ALogedSuccess,
ARejected: WordBool): WideString;
begin
with TADOQuery.Create(nil) do
try
Connection := conRead;
SQL.Add('SELECT Admin, FullName FROM Employees WHERE EmployeeRef=' +
QuotedStr(ARef) + ' AND Password=' + QuotedStr(APassword));
Open;
if Eof then
begin
Inc(ATimes);
ALogedSuccess := False;
if ATimes = 3 then
ARejected := True
else
ARejected := False;
AAdmin := False;
Result := '';
end
else
begin
ALogedSuccess := True;
ARejected := False;
AAdmin := FieldByName('admin').AsBoolean;
Result := fieldbyname('FullName').AsString;
end;
finally
Close;
Free;
end;
end;
initialization
TComponentFactory.Create(ComServer, TReadFromDatabase,
Class_ReadFromDatabase, ciMultiInstance, tmBoth);
end.
{Code 2: 运行不了的代码}
unit uRead;
{$WARN SYMBOL_PLATFORM OFF}
interface
uses
{...}
MidLayer_TLB,
DB,
ADODB,
Provider;
type
TReadFromDatabase = class(TMtsDataModule, IReadFromDatabase)
tblViewEmployees: TADOTable;
cdsEmployees: TClientDataSet;
dspEmployees: TDataSetProvider;
{...}
conRead: TADOConnection;
procedure MtsDataModuleCreate(Sender: TObject);
private
{ Private declarations }
protected
class procedure UpdateRegistry(Register: Boolean; const ClassID, ProgID:
string); override;
procedure PrepareEmployees; safecall;
procedure Login; safecall;
function SignIn(const ARef, APassword: WideString; ATimes: Integer;
var AAdmin, ALogedSuccess, ARejected: WordBool): WideString;
safecall;
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
end;
var
ReadFromDatabase: TReadFromDatabase;
implementation
uses
IniFiles,
uLog;
{$R *.DFM}
class procedure TReadFromDatabase.UpdateRegistry(Register: Boolean; const
ClassID, ProgID: string);
begin
if Register then
begin
inherited UpdateRegistry(Register, ClassID, ProgID);
EnableSocketTransport(ClassID);
EnableWebTransport(ClassID);
end
else
begin
DisableSocketTransport(ClassID);
DisableWebTransport(ClassID);
inherited UpdateRegistry(Register, ClassID, ProgID);
end;
end;
procedure TReadFromDatabase.PrepareEmployees;
begin
//reserved
end;
procedure TReadFromDatabase.Login;
begin
//reserved
end;
function TReadFromDatabase.SignIn(const ARef, APassword: WideString;
ATimes: Integer; var AAdmin, ALogedSuccess,
ARejected: WordBool): WideString;
begin
with TADOQuery.Create(nil) do
try
if not conRead.Connected then
conRead.Open();
Connection := conRead;
SQL.Add('SELECT Admin, FullName FROM Employees WHERE EmployeeRef=' +
QuotedStr(ARef) + ' AND Password=' + QuotedStr(APassword));
Open;
if Eof then
begin
Inc(ATimes);
ALogedSuccess := False;
if ATimes = 3 then
ARejected := True
else
ARejected := False;
AAdmin := False;
Result := '';
end
else
begin
ALogedSuccess := True;
ARejected := False;
AAdmin := FieldByName('admin').AsBoolean;
Result := fieldbyname('FullName').AsString;
end;
finally
Close;
Free;
end;
end;
constructor TReadFromDatabase.Create(AOwner: TComponent);
begin
end;
procedure TReadFromDatabase.MtsDataModuleCreate(Sender: TObject);
var
Loger : TLoger;
msg : WideString;
begin
// if conRead.Connected then
// msg := FormatDateTime('mm-dd hh:nn:ss', Now) + ': Opened on ' +
// conRead.ConnectionString
// else
// msg := FormatDateTime('mm-dd hh:nn:ss', Now) + ': is closed ';
// Loger.Log(msg);
// ShowMessage(msg);
end;
initialization
TComponentFactory.Create(ComServer, TReadFromDatabase,
Class_ReadFromDatabase, ciMultiInstance, tmBoth);
end.
先记录下来,过段时间再回头研究到底是什么问题。
没有评论:
发表评论