2016-06-21 35 views
-2

我設法將pdf文件保存到MS Access數據庫中,如下所示。但我不知道如何讀回,並保存爲一個文件Delphi 6.如何從MS Access讀回pdf數據

procedure TForm1.btnTask3Click(Sender: TObject); 
var 
    SL, splitString: TStringList; 
    i: Integer; 
    sourceFile, 
    sourceFile1, 
    outputFile : string; 
    firstName, 
    lastName, 
    userId : string; 
    dateOfBirth : TDate; 
    dbConnectionStrPath : string; 
begin 
    //set database connection string 
    dbConnectionStrPath := 'Provider=Microsoft.Jet.OLEDB.4.0;'+ 
      'Data Source='+GetCurrentDir+'\task3.mdb;'+ 
      'Persist Security Info=False'; 

    SL := TStringList.Create; 
    try 
    //Assign input and output paths 
    sourceFile := GetCurrentDir + '\source.txt'; 
    sourceFile1 := 'C:\Program Files\NetBeans 8.1\nb\shortcuts.pdf'; 
    outputFile := GetCurrentDir + '\output.txt'; 

    //Load the file into the stringlist 
    SL.LoadFromFile(sourcefile); 
    // For holding indivisual lines 
    splitString := TStringList.Create; 

    //Database stuff 
    //Set the connection string, table to save to and activate 
    ADOTable.ConnectionString := dbConnectionStrPath; 
    ADOTable.TableName := 'Patient'; 
    ADOTable.Active := True; 

    splitString.Delimiter := ','; 
    for i := 0 to SL.Count - 1 do 
    begin 

     // Split the line by the comma 
     splitString.DelimitedText := SL[i]; 

     //Assign each comma separeted value to a variable 
     firstName := splitString[0]; 
     lastName := splitString[1]; 
     userId  := splitString[2]; 
     dateOfBirth := StrToDate(splitString[3]); 

     //Assign values to the database table 
     ADOTable.Append; 
     ADOTable['UserId'] := userId; 
     ADOTable['firstname'] := firstName; 
     ADOTable['lastname'] := lastName; 
     ADOTable['DOB']  := dateOfBirth; 
     //Reference: http://stackoverflow.com/questions/4974259/access-2007-add-file-as-attachment-with-delphi 
     TBlobField(ADOTable.FieldByName('File')).LoadFromFile(sourceFile1); 

     //Save the data 
     ADOTable.Post; 

    end; 
    splitString.Free; 

    ShowMessage('Data saved to database'); 
    finally 
    SL.Free; 
    end; 
end; 

回答

2

嗯,你似乎已經想通了存儲在Blob字段確定一個文件,所以我不完全知道什麼(概念)您再次提取文件時遇到的問題,因爲您已經達到了90%。

閱讀在線幫助

TBlobField.SaveToFile()

顯然,然後才能使用該例程,您需要導航您的AdoTable到包含要提取斑點的紀錄。你可以做類似

if AdoTable.Locate('LastName;FirstName', VarArrayOf(['Doe', 'John']), []) then begin 
    // Save your blob to disk here. 
end; 

這就是你應該嘗試的。然後問一個新的問題,如果/當你卡住了。

順便說一下,你的q有哪些PDF標籤?您的代碼在文本文件上運行。

+0

這是回答你的問題還是你仍然卡住? – MartynA