2012-02-14 70 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;