Advertisement
arsel

Delphi BLOB

Jul 23rd, 2012
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. uses jpeg, ........
  2. type
  3. TForm1 = class(TForm)
  4. con1: TZConnection;
  5. qr1: TZQuery;
  6. qr1aaa: TStringField; //persistant field that only string value
  7. qr1gambar: TBlobField; //persistent field "gambar" that used to save JPEG image
  8. ds1: TDataSource;
  9. dlgOpen1: TOpenDialog;
  10. dbnvgr1: TDBNavigator;
  11. dbeaaa: TDBEdit; //use binding to connect to field "aaa"
  12. btn1: TButton;
  13. btn2: TButton;
  14. dbimg1: TDBImage;
  15. procedure btn1Click(Sender: TObject);
  16. procedure FormShow(Sender: TObject);
  17. procedure FormClose(Sender: TObject; var Action: TCloseAction);
  18. procedure btn2Click(Sender: TObject);
  19. private
  20. { Private declarations }
  21. public
  22. { Public declarations }
  23. end;
  24.  
  25. The code:
  26.  
  27. procedure TForm1.FormShow(Sender: TObject);
  28. begin
  29. //Connect to Database and open query
  30. con1.Connect;
  31. qr1.Close;
  32. qr1.Open;
  33. end;
  34.  
  35. procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
  36. begin
  37. //Close query and connection
  38. qr1.Close;
  39. con1.Disconnect;
  40. end;
  41.  
  42. procedure TForm1.btn1Click(Sender: TObject);
  43. begin
  44. if dlgOpen1.Execute then
  45. begin
  46. //Load JPEG image (or anything type of file) into database
  47. qr1gambar.LoadFromFile(dlgOpen1.FileName);
  48. end;
  49. end;
  50.  
  51. procedure TForm1.btn2Click(Sender: TObject);
  52. var j:TJPEGImage;
  53. begin
  54. //Display image from database to TDBimage (dbimg1) component
  55. j:=TJPEGImage.Create;
  56. j.Assign(qr1gambar);
  57. dbimg1.Picture.Assign(j);
  58. FreeAndNil(j);
  59. end;
  60.  
  61. How the code work?
  62. "btn1Click" method is clicked after the dataset is in edit or insert mode, then after it load from file then post it (I use DBNavigator in the above example). And then "btn2Click" is for display image from database to TDBImage component. Simply enough? yes, that's all.
  63.  
  64. Now, what about other format like Word, Excel or PDF document that you want to extract from the database? (the save method is same). The answer is, save it to file again. Zeos already implement it well, so you can do like this:
  65.  
  66. qr1gambar.SaveToFile('c:\somefile.doc');
  67.  
  68. Also simply enough? yes. So don't worry to use BLOB field with Zeos component :)
  69. How about to save/load it from Stream? You can also use "SaveToStream" and "LoadFromStream" method.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement