2012-02-14 104 views
1

我已存儲過程(從Postgres函數)類型的參數是這樣的:Blob to String:如何將Blob轉換爲PostgreSQL中StoredProcedure參數的字符串?

Params[0] : result = ftBlob // postgresql function = text 
Params[1] : 1 = ftString 
Params[2] : 2 = ftInteger 
Params[3] : 3 = ftInteger 

我的代碼是這樣的:

procedure TForm1.Button1Click(Sender: TObject); 
var 
    ResultStr: TResultStr; 
    BlobField: TBlobField; 
    bStream: TStream; 
    DataSet: TDataSet; 
    StoredProc: TSQLStoredProc; 
begin 
    sp01.Close; 
    sp01.Params[1].AsString := '2010/2011'; 
    sp01.Params[2].AsInteger := 2; 
    sp01.Params[3].AsInteger := 1; 
    sp01.ExecProc; 

    if sp01.ParamByName('result').Value.IsBlob then 
    begin 
    BlobField := StoredProc.ParamByName('result') as TBlobField; 
    bStream := sp01.CreateBlobStream(BlobField, bmRead); 
    try 
     bStream.Read(ResultStr,sizeof(TResultStr)); 
    finally 
     bStream.Free; 
    end; 
    end; 

    ShowMessage(ResultStr.Hasil); 
end; 

的問題是,如何做我想要得到的結果(Blob)成爲字符串?

回答

1

我不知道什麼是TResultString,但你可以用繩子做到這一點:

var 
    BlobResult: string; // Changed to make clearer where changes were below 
begin 
    // Your other code here 

    if sp01.ParamByName('result').Value.IsBlob then 
    begin 
    BlobField := StoredProc.ParamByName('result') as TBlobField; 
    bStream := sp01.CreateBlobStream(BlobField, bmRead); 
    try 
     SetLength(BlobResult, bStream.Size);   // Note changes here 
     bStream.Read(BlobResult[1], bStream.Size); // and here 
    finally 
     bStream.Free; 
    end; 
    end;