Created
December 11, 2022 17:34
-
-
Save MBulli/347a13d6d6322039e7f88fc93a6f8530 to your computer and use it in GitHub Desktop.
FireDAC BLOB streaming with MS SQL Server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
unit FireDacBlobStream; | |
interface | |
// https://docwiki.embarcadero.com/RADStudio/Alexandria/en/Support_for_Blob_Streaming_in_FireDAC | |
uses | |
Data.DB, | |
FireDAC.Comp.Client, | |
FireDAC.Phys.Intf, | |
FireDAC.Stan.Param, | |
System.Classes, | |
System.SysUtils; | |
procedure DownloadBlobStream(const FileName: string; DestStream: TStream; Connection : TFDConnection); | |
implementation | |
procedure DownloadBlobStream(const FileName: string; DestStream: TStream; Connection : TFDConnection); | |
begin | |
Connection.StartTransaction; | |
try | |
var Query := TFDQuery.Create(nil); | |
try | |
Query.SQL.Text := Format('SELECT :p = BulkColumn FROM OPENROWSET (BULK %s, SINGLE_BLOB) MyFile', [QuotedStr(FileName)]); | |
Query.Params[0].ParamType := ptOutput; | |
Query.Params[0].DataType := ftOraBlob; // ftOraBlob is correct, even we use MS SQL ... | |
Query.Params[0].AsStream := DestStream; | |
Query.Command.CommandKind := skExecute; | |
Query.Connection := Connection; | |
Query.ExecSQL; | |
Connection.Commit; | |
finally | |
FreeAndNil(Query); | |
// DestStream is now free'd by FireDAC | |
end; | |
except | |
Connection.Rollback; | |
raise; | |
end; | |
end; | |
end. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment