在同一个事务中,第一次执行的SQL修改数据后,在后续执行的SQL中体现不出来,请问有解决方法没有
//测试代码,以前的3.0版本里边,返回的是2个不同的编号,在现在版本中,返回的是同样的编号.
procedure TMAINFORM.Button1Click(Sender: TObject);
var
Cnn : Business.Data.TConnection;
H : Business.Data.TTransactionHandle;
begin
Cnn := Business.Data.TConnection.Create(nil);
Cnn.ConnectionString := 'DATABASEURL=Biz:\WDKJ\WDSJK.DATABASE';
Cnn.Open;
H := Cnn.Transaction.Start(False);
try
Edit1.Text := DBCommonUnit.TDBCommonUnit.RealCreateCode('A0001', Cnn);
Edit2.Text := DBCommonUnit.TDBCommonUnit.RealCreateCode('A0001', Cnn);
finally
Cnn.Transaction.Rollback(H);
end;
Cnn.Free;
end;
//SQL执行代码
static function TDBCOMMONUNIT.RealCreateCode(ACode : String;AConnection : TConnection = nil) : String;
const
//SELECT DHCODE, GS, ZHRQ, COUNTER, QZ FROM JCSJ_DJBMZDB WHERE DHCODE = HCODE
SQLText = 'SELECT DHCODE, GS, ZHRQ, COUNTER, QZ FROM JCSJ_DJBMZDB WHERE DHCODE = HCODE';
UpdateSQLText = 'UPDATE JCSJ_DJBMZDB SET ZHRQ = :ZHRQ, COUNTER = :COUNTER WHERE DHCODE = HCODE';
DHCODE = 'DHCODE';
var
lQuery: Business.Data.TQuery;
Ld, Cd : TDateTime;
QZ, FMT : String;
Counter : Integer;
begin
lQuery := Business.Data.TQuery.Create(nil);
with lQuery do
try
if AConnection = nil then
ConnectionString := DBConnectString
else
Connection := AConnection;
Close;
CommandText := SQLText; // 从通用编码表中取出当前编号的数据
Params.ParamByName(DHCODE).AsString := ACode;
Open;
if (not lQuery.IsEmpty) then
begin
if FieldByName('COUNTER').IsNull then
Counter := 0
else
Counter := FieldByName('COUNTER').AsInteger;
CD := GetServerTime;
QZ := FieldByName('QZ').AsString;
if FieldByName('ZHRQ').IsNull then
Ld := Cd
else
Ld := FieldByName('ZHRQ').AsDateTime;
FMT := FieldByName('GS').AsString;
if SysUtils.UpperCase(StrUtils.LeftStr(FMT, 3)) = 'YMD' then
begin
if Ld <> Cd then
Counter := 1
else
Counter := Counter + 1;
end
else if SysUtils.UpperCase(StrUtils.LeftStr(FMT, 2)) = 'YM' then
begin
if (Ld-DateUtils.DayOf(ld)) <> (cd - DateUtils.DayOf(cd)) then
Counter := 1
else
Counter := Counter + 1;
end
else
Counter := Counter + 1;
Result := DateToCodeStr(QZ, cd, Counter, FMT);
Close;
CommandText := UpdateSQLText;
Params.ParamByName('ZHRQ').AsDateTime := Cd;
Params.ParamByName('COUNTER').AsInteger := Counter;
Execute;
end
else
Raise Exception.Create('单据编码规则('+ACode+')不存在');
finally
lQuery.Free;
end;
end; |