Author: Tomas Rutkauskas
How can I write and read to/ from a blob field? I want to save OLE objects like
Excel sheets or wav sound into a Paradox table rather than to a file.
Answer:
Solve 1:
1 procedure TForm1.bnSaveClick(Sender: TObject);
2 begin
3 MediaPlayer1.Close;
4 tabWaves.Append;
5 {tabWavesAudioData is a TBlobField}
6 tabWavesAudioData.LoadFromFile(MediaPlayer1.FileName);
7 {MediaPlayer1.FileName is path to wave file}
8 tabWavesDescription.Value := MediaPlayer1.FileName;
9 tabWaves.Post;
10 end;
11
12 procedure TForm1.LoadWaveFromTable;
13 begin
14 MediaPlayer2.Close;
15 {Save the Blob to a file}
16 tabWavesAudioData.SaveToFile('C:\Temp\WAVETABLE.wav');
17 {Load wave into MediaPlayer}
18 MediaPlayer2.FileName := 'C:\Temp\WAVETABLE.wav';
19 MediaPlayer2.Open;
20 end;
Solve 2:
21 uses
22 MMSystem;
23
24 procedure TFrmPlayWaveFromDB.BtnWaveToDBClick(Sender: TObject);
25 var
26 theBStream: TBlobStream;
27 begin
28 if FlbSelectWave.Filename <> EmptyStr then
29 begin
30 FMemStream.Seek(0, soFromBeginning);
31 FMemStream.LoadFromFile(FlbSelectWave.Filename);
32 Table1.Edit;
33 theBStream := TBlobStream.Create(Table1ABlobField, bmReadWrite);
34 try
35 FMemStream.SaveToStream(theBStream);
36 finally
37 theBStream.Free;
38 end;
39 Table1.Post;
40 end;
41 end;
42
43 procedure TFrmPlayWaveFromDB.FormCreate(Sender: TObject);
44 begin
45 {The MemoryStream must persist while the sound is being played, so it can't be a
46 local variable}
47 FMemStream := TMemoryStream.Create;
48 Table1.Open;
49 end;
50
51 procedure TFrmPlayWaveFromDB.BtnPlayWaveClick(Sender: TObject);
52 var
53 theBStream: TBlobStream;
54 begin
55 theBStream := TBlobStream.Create(Table1ABlobField, bmRead);
56 try
57 FMemStream.LoadFromStream(theBStream);
58 sndPlaySound(FMemStream.Memory, SND_ASYNC or SND_MEMORY);
59 finally
60 theBStream.Free;
61 end;
62 end;
63
64 procedure TFrmPlayWaveFromDB.FormDestroy(Sender: TObject);
65 begin
66 FMemStream.Free;
67 Table1.Close;
68 end;
69
70 procedure TFrmPlayWaveFromDB.FlbSelectWaveChange(Sender: TObject);
71 begin
72 if FlbSelectWave.Filename <> EmptyStr then
73 BtnWaveToDB.Enabled := true
74 else
75 BtnWaveToDB.Enabled := false;
76 end;
Solve 3:
77 unit Main;
78
79 interface
80
81 uses
82 Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
83 ExtCtrls, DBCtrls, DB, DBTables, StdCtrls, Mask, Buttons, ComCtrls;
84
85 type
86 TMainForm = class(TForm)
87 tblSounds: TTable;
88 dsSounds: TDataSource;
89 tblSoundsWaveTitle: TStringField;
90 tblSoundsWave: TBlobField;
91 edTitle: TDBEdit;
92 edFileName: TDBEdit;
93 Label1: TLabel;
94 Label2: TLabel;
95 OpenDialog: TOpenDialog;
96 tblSoundsFileName: TStringField;
97 SaveDialog: TSaveDialog;
98 pnlToobar: TPanel;
99 sbPlay: TSpeedButton;
100 sbAdd: TSpeedButton;
101 sbSave: TSpeedButton;
102 sbExit: TSpeedButton;
103 Bevel1: TBevel;
104 dbnNavigator: TDBNavigator;
105 stbStatus: TStatusBar;
106 procedure sbPlayClick(Sender: TObject);
107 procedure sbAddClick(Sender: TObject);
108 procedure sbSaveClick(Sender: TObject);
109 procedure sbExitClick(Sender: TObject);
110 procedure FormCreate(Sender: TObject);
111 private
112 procedure OnAppHint(Sender: TObject);
113 end;
114
115 var
116 MainForm: TMainForm;
117
118 implementation
119
120 {$R *.DFM}
121
122 uses
123 MMSystem;
124
125 procedure TMainForm.sbPlayClick(Sender: TObject);
126 var
127 B: TBlobStream;
128 M: TMemoryStream;
129 begin
130 B := TBlobStream.Create(tblSoundsWave, bmRead); {create blob stream}
131 Screen.Cursor := crHourGlass; {wait hourglass}
132 try
133 M := TMemoryStream.Create; {create memory stream}
134 try
135 M.CopyFrom(B, B.Size); {copy from blob to memory stream}
136 {Attempt to play sound. Raise exception if something goes wrong}
137 Win32Check(PlaySound(M.Memory, 0, SND_SYNC or SND_MEMORY));
138 finally
139 M.Free;
140 end;
141 finally
142 Screen.Cursor := crDefault;
143 B.Free;
144 end;
145 end;
146
147 procedure TMainForm.sbAddClick(Sender: TObject);
148 begin
149 if OpenDialog.Execute then
150 begin
151 tblSounds.Append;
152 tblSounds['FileName'] := ExtractFileName(OpenDialog.FileName);
153 tblSoundsWave.LoadFromFile(OpenDialog.FileName);
154 edTitle.SetFocus;
155 end;
156 end;
157
158 procedure TMainForm.sbSaveClick(Sender: TObject);
159 begin
160 with SaveDialog do
161 begin
162 FileName := tblSounds['FileName']; {initialize file name}
163 if Execute then {execute dialog}
164 tblSoundsWave.SaveToFile(FileName); {save blob to file}
165 end;
166 end;
167
168 procedure TMainForm.sbExitClick(Sender: TObject);
169 begin
170 Close;
171 end;
172
173 procedure TMainForm.FormCreate(Sender: TObject);
174 begin
175 Application.OnHint := OnAppHint;
176 end;
177
178 procedure TMainForm.OnAppHint(Sender: TObject);
179 begin
180 stbStatus.SimpleText := Application.Hint;
181 end;
182
183 end.
|