7
0

More than 1 year has passed since last update.

Delphi / FireDAC MS Access で、画像の保存と取り出しのテストをしてみました。

Posted at

はじめに

Delphi Advent Calendar 2022 12/05と12/07と12/08と12/11の記事のMicrosoft Access版です。

1. テストするWindowsのローカルの機器には、Microsoft Access はインストールされていない状態でテストしていきます。

実行するとエラーとなりますので、

[FireDAC][Phys][ODBC][Microsoft][ODBC Microsoft Access Driver] データベース '(不明)' を開くことができません。アプリケーションで認識できないデータベースであるか、またはファイルが破損しています。
02_1.png
Access Runtime 2016(https://www.microsoft.com/ja-jp/download/details.aspx?id=50040)のインストールが必要です。

2. Microsoft Access のデータベースファィル に事前に test_1.accdb を他のAccessのライセンスあり機器で作成したものを使用します

00_ver.png

3. Microsoft Accessでは、データ型は Image です

4. Delphiで、uses JPEG, GIFImg, PNGImage ; //使用する画像を宣言

Delphi 10.2でテストしています。
01_ide.png


以下サンプルコード

画像データの追加のサンプルコード

FD_MSSQL2019_B_Unit.pas
procedure TForm1.SpeedButton6Click(Sender: TObject);
var
  SQLstmt:String;
begin
  with FDConnection1 do
  try
    SQLstmt :=' INSERT INTO '
             +' test_tbl ( ts_no  , tsblob ) '
             +'	VALUES ('
             +' :ts_no , :tsblob '
             + ')';
    FDQuery1.SQL.Clear;
    FDQuery1.SQL.Text:= sqlstmt;
    FDQuery1.ParamByName('ts_no').AsInteger:=StrToInt( Edit_TS_NO.Text );
    FDQuery1.ParamByName('tsblob').LoadFromFile( Edit_Files.Text ,ftBlob);
    FDQuery1.ExecSQL();
    ShowMessage('登録しました');
  finally
    FDQuery1.Close;
  end;
'''

### 画像データの表示のサンプルコード
```Delphi:FD_MSSQL2019_B_Unit.pas
procedure TForm1.SpeedButton7Click(Sender: TObject);
var
  SQLstmt:String;
begin
  with FDConnection1 do
  try
    SQLstmt := 'SELECT * FROM test_tbl '
            +  'WHERE ts_no = :ts_no';
    FDQuery1.SQL.Clear;
    FDQuery1.SQL.Text:= sqlstmt;
    FDQuery1.ParamByName('ts_no').AsInteger:=StrToInt(Edit_ts_no.Text);
    FDQuery1.Open();
    if not FDQuery1.Eof then
    begin
      Edit_ts_no.Text  :=IntToStr(FDQuery1.FieldByName('ts_no').AsInteger);
      Image1.Picture.Assign( TBLOBField(FDQuery1.FieldByName('tsblob') ) );
    end;
  finally
    FDQuery1.Close;
  end;
end;

テーブルの作成のサンプルコード

FD_MSSQL2019_B_Unit.pas

procedure TForm1.SpeedButton1Click(Sender: TObject);
begin
  FDConnection1.Params.Values['Server']      :=LabeledEdit1.Text;
  if LabeledEdit1.Text='127.0.0.1' then
    FDConnection1.Params.Values['Database'] := GetCurrentDir+'\'+LabeledEdit2.Text // 'sq3_sales.s3db');   //System.IOUtils
  else
    FDConnection1.Params.Values['Database']    :=LabeledEdit2.Text;

//  FDConnection1.Params.Values['User_Name']   :=LabeledEdit3.Text;
//  FDConnection1.Params.Values['Password']    :=LabeledEdit4.Text;
//  FDConnection1.Params.Values['CharacterSet']:=LabeledEdit5.Text;
  FDConnection1.Params.Values['OpenMode']:=LabeledEdit5.Text;
  FDPhysMSAccessDriverLink1.VendorLib        :=LabeledEdit6.Text;
  FDConnection1.Params.Values['Port']        :=LabeledEdit7.Text;

  FDConnection1.Connected:=True;
  if FDConnection1.Connected=True  then Label1.Caption:='Connect Success'
  else                                  Label1.Caption:='Not connected';
end;

今回は、テストするWindowsのローカルの機器には、Microsoft Access はインストールされていない状態なので、データベースを参照ツールとして、有名な A5:SQL Mk-2 フリーのSQLクライアント/ER図作成ソフト を使用します、図のように接続できるので、非常にありがたいツールです。
27.png

以下手順 A5:SQL Mk-2 の詳細な手順は、検索でも手数ヒットしますし、A5:SQL Mk-2サイトにも豊富にドキュメントがあります。
36.png

今は、ローコード/ノーコードが流行ですが、過去の業務システムで、Microsoft Accessを使用されているものも、いまだ多数存しているのではないでしょうか?

Microsoft Accessのインストールされていない状態で、Microsoft Accessのデータを参照する場合の参考になれば幸いです。

このソースのダウロードは、ここからできます

7
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
0