代码如下:
MTS线程模式是:Apartment
交易模式是:Requires a Transaction
============================================
实现代码
=============================================
unit uMTSObject;
{$WARN SYMBOL_PLATFORM OFF}
interface
uses
ActiveX, Mtsobj, Mtx, ComObj, PMTSObject_TLB, StdVcl,Global,DB,ADODB,
Sysutils;
type
TMTSObject = class(TMtsAutoObject, IMTSObject)
protected
function getConnectionString: WideString;
safecall;
function getTheCarInfo(const theCar: WideString): WideString;
safecall;
function CheckRequest(const value: WideString): WideString;
safecall;
function ExecutePay(const EndDate, TheCar: WideString): WideString;
safecall;
public
procedure Initialize;
override;
procedure ConnectionDb;
{ Protected declarations }
end;
var
ConnDB:TADOConnection=nil;
implementation
uses ComServ,uDataModule;
//取得数据库连接字符串
function TMTSObject.getConnectionString: WideString;
begin
if Assigned(ConnDB) then
result:=ConnDB.ConnectionString;
end;
//取得车辆信息,theCar:车牌号
function TMTSObject.getTheCarInfo(const theCar: WideString): WideString;
var
Query:TADOQuery;
begin
result:='';
Query:=nil;
try
Query:=TADOQuery.create(nil);
Query.Connection:=ConnDB;
with Querydo
begin
close;
Sql.Clear;
SQL.Text:='SELECT b.* ,name,sys_yl,sys_jj'+
' FROM owner a ,vehicle b WHERE car_num ='+''''+theCar+''''+
' AND a.id=b.id'+
' AND b.flag_yd <>''2'''+
' AND b.flag_yd <>''4''';
prepared;
Open;
if not eof then
result:=format(theCarInfoContent,[fieldbyName('car_num').asString,
fieldbyName('id').asString,
fieldbyName('name').asString,
fieldbyName('date_rh').asString,
fieldbyName('date_cc').asString,
fieldbyName('model').asString,
fieldbyName('Engine_Num').asString,
fieldbyName('bone_Num').asString,
fieldbyName('car_type').asString,
fieldbyName('load_hd').asString,
fieldbyName('seat_f').asString,
fieldbyName('seat_b').asString,
fieldbyName('flag_kh').asString,
fieldbyName('load_jz').asString,
fieldbyName('seat_jz').asString,
fieldbyName('flag_yy').asString,
fieldbyName('flag_yd').asString,
fieldbyName('flag_jz').asString,
fieldbyName('certificate').asString,
fieldbyName('cert_Num').asString,
fieldbyName('deadline').ASString]);
end;
setComplete;
finally
Query.Close;
FreeAndNil(Query);
end;
end;
//连接数据库
procedure TMTSObject.ConnectionDb;
begin
if not Assigned(connDB) then
ConnDB:=TADOConnection.Create(nil);
connDB.connectionString:=Format(sLinkSQLDB,[readRegistry('MachineName'),
readRegistry('DBName'),readRegistry('UserName'),readRegistry('password')]);
try
connDB.Close;
ConnDB.LoginPrompt:=false;
ConnDB.Connected:=true;
except
end;
end;
procedure TMTSObject.Initialize;
begin
Inherited;
ConnectionDb;
end;
//判断当前值是否符合要求,返回错误信息
function TMTSObject.CheckRequest(const value: WideString): WideString;
begin
end;
function TMTSObject.ExecutePay(const EndDate,
TheCar: WideString): WideString;
var
Query:Tadoquery;
enddatetime:Tdatetime;
spvehicle:TwebVehicle;
begin
result:='';
Query:=nil;
try
Query:=Tadoquery.Create(nil);
Query.Connection:=ConnDB;
with Querydo
begin
close;
Sql.Clear;
Sql.text:='select * from vehicle where car_num=:car_num';
Parameters.ParamByName('car_num').Value:=thecar;
prepared;
Open;
spvehicle.CarNum:=thecar;
spvehicle.CarType:=fieldbyname('flag_kh').AsString;
if spvehicle.CarType='1' then
spvehicle.Seat_hd:=fieldbyname('seat_f').asfloat
else
spvehicle.Seat_hd:=fieldbyname('load_hd').asfloat;
spvehicle.DeadLine:=fieldbyname('deadline').AsDateTime;
spvehicle.startdate:=fieldbyname('deadline').asdatetime+1;
spvehicle.Certificate:=fieldbyname('Certificate').asstring[1];
end;
enddatetime:=strTodatetime(enddate);
spvehicle.monthnumber:=returnmonth_number(spvehicle.startdate,enddatetime);
spvehicle.xuNnumber:=returnxuN_number(spvehicle.startdate,enddatetime);
spvehicle.standard:=RETURNSF_STANDARD(spvehicle.cartype,query);
spvehicle.pay_yl:=(spvehicle.standard*spvehicle.seat_hd*spvehicle.monthnumber) //月金额
+(round((spvehicle.standard*spvehicle.seat_hd)/3)*spvehicle.xuNnumber);
//旬金额
result:=format(TheCarinfopayContent,[floatTostr(spvehicle.pay_yl)]);
setComplete;
finally
Query.close;
Query.free;
end;
end;
initialization
TAutoObjectFactory.Create(ComServer, TMTSObject, Class_MTSObject,
ciMultiInstance, tmApartment);
CoInitialize(nil);
finalization
CoUninitialize;