Data.DB.pas 的 BUG
本帖最后由 sczyq 于 2013-08-02 21:34:24 编辑 这段运行结果是正确的
//---------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
AnsiString S = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
TParams * AParams = new TParams(this);
TParam * AParam = AParams->AddParameter();
AParam->Name = "Name";
AParam->Value = S;
TValueBuffer ABuffer;
ABuffer.Length = AParam->GetDataSize();
AParam->GetData(ABuffer);
S = "1234567890";
AParam->Value = S;
AParam->SetData(ABuffer);
Memo1->Lines->Add(AParam->AsString); // 结果正确
delete AParams;
}
//---------------------------------------
//---------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
String S = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
TParams * AParams = new TParams(this);
TParam * AParam = AParams->AddParameter();
AParam->Name = "Name";
AParam->Value = S;
TValueBuffer ABuffer;
ABuffer.Length = AParam->GetDataSize();
AParam->GetData(ABuffer);
S = "1234567890";
AParam->Value = S;
AParam->SetData(ABuffer);
Memo1->Lines->Add(AParam->AsString); // 结果错误
delete AParams;
}
//---------------------------------------
procedure TParam.GetData(Buffer: TValueBuffer);
... 省略
case DataType of
...
ftWideString, ftFixedWideChar, ftWideMemo:
begin
TempBytes := TEncoding.Default.GetBytes(GetAsWideString); //原因是用 Default
Move(TempBytes[0], Buffer[0], Length(TempBytes));
end;
...
end;
[解决办法]
我试了下:
TParam.GetData(Buffer: TValueBuffer);总是取得Ansi编码,导致错误。
GetDate(void *buff) 却是正确的。不过却被描述为deprected,过时了不建议再用。
一般很少用 GetData SetData ,直接用一个TParam 或者 Value 来保存(恢复)参数
当要用到时,用不建议的GetDate(void *buff) SetDate(void *buff) 就不会做编码转换。
真是:不建议用的还正确,新的BUG了。
SetDate(void *buff)不安全,没废掉前,也少用它。
上面的代码改成:
AParam->GetData(&ABuffer[0]);//这样用的是 GetDate(void *buff)
结果就好了