Use ADO to connect to an Access Database and write a BlobStream value
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
unit Unit1;interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ADODB, DB, DBTables, ComObj; type TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; function ConnectToADODB(var Query: TADOQuery; ConnectStr: String): Boolean; Overload; function UpdateBlob(Connection: TADOConnection; Spalte: String; Tabelle: String; Where: String; var ms: TMemoryStream): Boolean; procedure ShowEOleException(AExc: EOleException; Query: String); var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject); var Query: TADOQuery; ms: TMemoryStream; ConnectStr: String; begin ms := TMemoryStream.Create; ms.LoadFromFile('d:a.txt'); Query := TADOQuery.Create(nil); // You must connect to AccessDB first. // See: Query.Connection, TADOConection or Query.ConnectionString //my function to connect to DB ConnectStr := 'Provider=Microsoft.Jet.OLEDB.4.0;' + // provider for Access2000 'Data Source=C:db1.mdb;' + // databasefile 'Mode=ReadWrite|Share Deny None;' + // set to ReadWrite 'Persist Security Info=False'; if not ConnectToADODB(Query, ConnectStr) then ShowMessage('Connecting to DB failed.'); // data is my row and email the table UpdateBlob(Query.Connection, 'blobfieldname', 'Tabelle1', 'id=1', ms); ms.Free; // disconnect from DB Query.Connection.Close; Query.Free; end; function ConnectToADODB(var Query: TADOQuery; ConnectStr: String): Boolean; Overload; begin Query.Connection := TADOConnection.Create(nil); Query.Connection.LoginPrompt := True; Query.Connection.ConnectionString := ConnectStr; Query.Connection.Open; result := Query.Connection.Connected; end; function UpdateBlob(Connection: TADOConnection; Spalte: String; Tabelle: String; Where: String; var ms: TMemoryStream): Boolean; var BlobField: TBlobField; Table: TADOTable; begin result := True; try ms.Seek(0, soFromBeginning); Table := TADOTable.Create(nil); Table.Connection := Connection; Table.TableName := Tabelle; Table.Filtered := False; // Set Filter like SQL-Command '... WHERE id=1' Table.Filter := Where; Table.Filtered := True; Table.Open; Table.First; if not Table.FieldByName(Spalte).IsBlob then Raise EOleException.Create('The field ' + Spalte + ' is not a blob-field.', S_FALSE, 'ITSQL.UpdateBlob', '', 0); BlobField := TBlobField(Table.FieldByName(Spalte)); Table.Edit; BlobField.LoadFromStream(ms); Table.Post; Table.Free; except on E: EOleException do begin ShowEOleException(E, 'UPDATE BLOB FROM: SELECT ' + Spalte + ' FROM ' + Tabelle + ' WHERE ' + Where); result := False; end; end; end; procedure ShowEOleException(AExc: EOleException; Query: String); var ErrShowFrm: TForm; Memo: TMemo; begin ErrShowFrm := TForm.Create(nil); ErrShowFrm.Position := poScreenCenter; ErrShowFrm.Width := 640; ErrShowFrm.Height := 480; Memo := TMemo.Create(ErrShowFrm); Memo.Parent := ErrShowFrm; Memo.Align := alClient; Memo.Lines.Clear; Memo.Lines.Add('Message: ' + AExc.Message); Memo.Lines.Add(' Source: ' + AExc.Source); Memo.Lines.Add(' ClassName: ' + AExc.ClassName); Memo.Lines.Add(' Error Code: ' + IntToStr(AExc.ErrorCode)); Memo.Lines.Add(' Query: ' + Query); ErrShowFrm.ShowModal; Memo.Free; ErrShowFrm.Free; end; end. |