?
Solved

Inserting JPG images to an MSSQL 2000 table

Posted on 2005-04-06
3
Medium Priority
?
301 Views
Last Modified: 2010-04-05
Hi Guys,

Given an MSSQL server 2000 database, MYDATABASE that contains a table EMPPICS
whose fields are:

EMPPICSKEY  INT (not null) (Primarykey)
EMPLOYEEKEY INT (not null)
PICTURE  IMAGE (not null)

On my local harddrive, say at "c:\emppics\" , I have stored here employees images/pictures (JPG) whose filename corresponds to the actual employeekey found on EMPLOYEE table which resides on the same database mentioned above.
Sample JPG files are:

1.jpg
15.jpg
21.jpg
30.jpg

Can anybody suggest a delphi 6 function that will take the images/pictures path as parameter, read all JPG files on this directory and then insert/add these automatically to the EMPPICS table?

Where:

EMPPICS.EMPLOYEEKEY  = JPGs filename
EMPPICS.PICTURE = the actual JPG image

So it will be something like:

EMPPICS TABLE

EMPPICSKEY     EMPLOYEEKEY    PICTURE
=================================
        1                        1                 (...)
        2                       15                (...)
        3                        21                (...)
        4                        30                (...)
==================================

If this can be done without using any 3rd party component at all, so much better.


Please Help,
zerobro


0
Comment
Question by:zerobro
  • 2
3 Comments
 
LVL 15

Expert Comment

by:mikelittlewood
ID: 13715306
Get jpg files from a directory

procedure TForm1.Search(dir : string);
var
   sr : TSearchRec;
   result : integer;
begin
     result := findFirst(dir+'*.*',faAnyfile,sr);
     while result = 0 do
     begin
          if (sr.name <> '.')and(sr.name <> '..')then
          begin
               if (sr.attr and faDirectory > 0) then
                  search(dir+sr.name+'\');
               slFiles.add(dir+sr.name);
          end;
          result := findNext(sr);
     end;
     findClose(sr);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  slFiles := TStringList.Create;
  Search('c:\');
end;
0
 
LVL 15

Accepted Solution

by:
mikelittlewood earned 300 total points
ID: 13715404
Ok Im fairly sure Im not saving the image to the database correctly using the table, but I think Im 90% there.
Have a look at other PAQs on saving image as a blob

const
  cPath = 'C:\emppics\';

procedure TForm1.Search(dir : string);
var
   sr : TSearchRec;
   result : integer;
begin
     result := findFirst(dir+'*.*',faAnyfile,sr);
     while result = 0 do
     begin
          if (sr.name <> '.')and(sr.name <> '..')then
          begin
               if (sr.attr and faDirectory > 0) then
                  search(sr.name+'\');
               slFiles.add(sr.name);
          end;
          result := findNext(sr);
     end;
     findClose(sr);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  slFiles := TStringList.Create;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  iLoop: Integer;
  sKey: String;
  bs: TBlobStream;
  fs: TFileStream;
begin
  // grab a list of jpg files
  Search(cPath);
  // loop through the files
  for iLoop := 0 to Pred(slFiles.Count) do
  begin
    // strip out the key from the filename
    sKey := Copy(slFiles[iLoop], 1, Pos('.jpg', slFiles[iLoop])-1);
    With ADOTable do
      try
        // open the table
        Open;
        // locate the key of the current file
        if Locate('EMPLOYEEKEY', sKey, [loCaseInsensitive]) then
        begin
          // load the image to the filestream
          fs := TFileStream.Create(cPath+slFiles[iLoop], fmOpenRead);
          bs := TBlobStream(CreateBlobStream(FieldByName('PICTURE'), bmRead));
          bs.CopyFrom(fs,0);
          bs.Free;
          fs.Free;
          Post;
        end;
      finally
        // close the table
        Close;
      end;
  end;
end;
0
 

Assisted Solution

by:scotdance
scotdance earned 100 total points
ID: 13716522
Hi,
i had a similar problem a few months back, have a look at this question as this was where i got my solution from.


http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_11565459.html


Regards
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question