Visit our Sponsor   Visit our Sponsor
delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi







Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (0)


How to deal with OLE DB directly, using its interfacesGo to Alex Wijoyo's websiteComponent available for this articleFormat this article printer-friendly!Bookmark function is only available for registered users!
Connect to Interbase/Firebird using Interbase OLE DB provider (no ODBC)
Product:
Delphi 5.x (or higher)
Category:
ADO/OLE-DB
Skill Level:
Scoring:
Last Update:
08/20/2001
Search Keys:
delphi delphi3000 article borland vcl code-snippet adoint.pas connection command recordset ado ole-db interbase firebird
Times Scored:
3
Visits:
7168
Uploader: Alex Wijoyo
Company: Excellont
Reference: N/A
Component Download: ../article/2604/SIBProvider.zip
 
Question/Problem/Abstract:
An example for how to deal with OLE DB directly, using its interfaces.
Answer:



Sometimes we need to deal with OLE DB directly without using data aware components. Interfaces provided by Microsoft allow us work with them easily. And Borland has translates them into Delphi in ADOINT.PAS. Because I love Interbase/Firebird then I try to search the Internet for free Interbase OLE DB provider. And I found SIBProvider, here are the information about it (I have no relationship with SIBProvider).
SIBPROvider - Interbase OLE DB Provider v. 1.00
Web site: www.sibprovider.com
Developed by: Marcio Wesley Borges
e-mail: marciowb@technologist.com
ICQ#: 105147566

This OLE DB Provider works fine for my example but didn’t work for ADOExpress and I don’t know why. OK enough talking here’s the source code. Thank you to delphi3000 admins, you can download the source code at the link above.

unit ufrmProject;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ExtCtrls, Grids, ADOInt;

type
  TfrmProject = class(TForm)
    pnlRight: TPanel;
    pnlLeft: TPanel;
    strgrdMaster: TStringGrid;
    spltrMiddle: TSplitter;
    strgrdDetail: TStringGrid;
    btnPopulate: TButton;
    btnCommit: TButton;
    btnRollback: TButton;
    btnAppendMstr: TButton;
    btnDeleteMstr: TButton;
    procedure FormCreate(Sender: TObject);
    procedure btnPopulateClick(Sender: TObject);
    procedure strgrdMasterSelectCell(Sender: TObject; ACol, ARow: Integer;
      var CanSelect: Boolean);
    procedure strgrdMasterSetEditText(Sender: TObject; ACol, ARow: Integer;
      const Value: String);
    procedure btnCommitClick(Sender: TObject);
    procedure btnRollbackClick(Sender: TObject);
    procedure btnAppendMstrClick(Sender: TObject);
    procedure btnDeleteMstrClick(Sender: TObject);
  private
    { Private declarations }
  public
    cmd:_Command;
    con:_Connection;
    rs:_RecordSet;
    proj_id:string;//last row project id
    mstLastRow:integer;
    mstEdits:array of boolean;//which columns of last row are edited
  end;

var
  frmProject: TfrmProject;

implementation

{$R *.DFM}

procedure TfrmProject.FormCreate(Sender: TObject);
begin
//create instances using their factories
  con:=CoConnection.Create;
  cmd:=CoCommand.Create;
  rs:=CoRecordSet.Create;
  with con do
  begin
    Provider:='sibprovi.SIBProvider';
    ConnectionString:='Data Source=C:\Temp\Employee.gdb;Location=localhost:;Extended Properties="";Persist Encrypted=True;Encrypt Password=True;Mask Password=True;Cache Authentication=True;Persist Security Info=True;CHARACTER SET="";ROLE=""';
    Open(ConnectionString,'sysdba','masterkey',0);
    BeginTrans;
  end;
  cmd.Set_ActiveConnection(con);
  strgrdMaster.ColWidths[0]:=20;
  strgrdDetail.ColWidths[0]:=20;
//make last row invalid
  mstLastRow:=-1;
end;

procedure TfrmProject.btnPopulateClick(Sender: TObject);
var
  i,j,upr:integer;
  recAfctd:OleVariant;
  flag:boolean;
begin
  with cmd do
  begin
    CommandText:='select a.*,b.full_name from project a left join employee b on b.emp_no=a.team_leader';
    rs:=Execute(recAfctd,EmptyParam,0);
  end;
  with rs,strgrdMaster do
  begin
//clean up the grid's row
    RowCount:=2;
//no column has edited
    SetLength(mstEdits,Fields.Count);
    ColCount:=Fields.Count+1;
    upr:=Fields.Count-1;
//set column's name
    for i:=0 to upr do
    begin
      mstEdits[i]:=false;
      Cells[i+1,0]:=Fields[i].Name;
      Cells[i+1,1]:='';
    end;
    i:=0;
    while not Eof do
    begin
      inc(i);
      RowCount:=i+1;
      for j:=0 to upr do
        if VarIsNull(Fields[j].Value) then
          Cells[j+1,i]:=''
        else
          Cells[j+1,i]:=Fields[j].Value;
      MoveNext;
    end;
    Close;
  end;
//populate detail by imitating select cell
  if i>0 then
    with strgrdMaster do
      strgrdMasterSelectCell(strgrdMaster,1,1,flag);
end;

procedure TfrmProject.strgrdMasterSelectCell(Sender: TObject; ACol,
  ARow: Integer; var CanSelect: Boolean);
var
  i,j,upr:integer;
  recAfctd:OleVariant;
  anyEdit:boolean;
  updStmt,temp:string;
  grid:TStringGrid;
begin
//row change detected
  if (ARow<>mstLastRow)and(ARow>0) then
  begin
    anyEdit:=false;
    grid:=TStringGrid(Sender);
    upr:=grid.ColCount-2;
//are there any edits
    for i:=0 to upr do
      if mstEdits[i] then
      begin
        anyEdit:=true;
        break;
      end;
    if anyEdit then
    begin
//it has been updated
      if proj_id<>'' then
      begin
//bla, bla, bla and presto here comes the update statement
        updStmt:='update project set ';
        temp:='';
        for i:=0 to upr do
          if mstEdits[i] then
          begin
            if temp<>'' then
              temp:=temp+',';
            temp:=temp+grid.Cells[i+1,0]+'=:'+grid.Cells[i+1,0];
          end;
        updStmt:=updStmt+temp;
        updStmt:=updStmt+' where proj_id=:proj_id';
        with cmd do
        begin
          CommandText:=updStmt;
          Parameters.Refresh;
          for i:=0 to upr do
            if mstEdits[i] then
            begin
              Parameters[grid.Cells[i+1,0]].Value:=grid.Cells[i+1,mstLastRow];
              mstEdits[i]:=false;
            end;
          Parameters['proj_id'].Value:=proj_id;
          Execute(recAfctd,EmptyParam,0);
        end;
      end
//it has been inserted
      else
      begin
//generate insert statement
        updStmt:='insert into project (';
        temp:='';
        for i:=0 to upr do
          if mstEdits[i] then
          begin
            if temp<>'' then
              temp:=temp+',';
            temp:=temp+':'+grid.Cells[i+1,0];
          end;
        updStmt:=updStmt+StringReplace(temp,':','',[rfReplaceAll])+') values (';
        updStmt:=updStmt+temp+')';
        with cmd do
        begin
          CommandText:=updStmt;
          Parameters.Refresh;
          for i:=0 to upr do
            if mstEdits[i] then
            begin
              Parameters[grid.Cells[i+1,0]].Value:=grid.Cells[i+1,mstLastRow];
              mstEdits[i]:=false;
            end;
          Execute(recAfctd,EmptyParam,0);
        end;
      end
    end;
//get new project id
    proj_id:=TStringGrid(Sender).Cells[1,ARow];
//populate detail using new project id
    with cmd do
    begin
      CommandText:='select a.*,b.full_name from employee_project a,employee b where a.proj_id=:proj_id and b.emp_no=a.emp_no';
      Parameters.Refresh;
      Parameters['proj_id'].Value:=proj_id;
      rs:=Execute(recAfctd,EmptyParam,0);
    end;
    with rs,strgrdDetail do
    begin
      RowCount:=2;
      ColCount:=Fields.Count+1;
      upr:=Fields.Count-1;
      for i:=0 to upr do
      begin
        Cells[i+1,0]:=Fields[i].Name;
        Cells[i+1,1]:='';
      end;
      i:=0;
      while not Eof do
      begin
        inc(i);
        RowCount:=i+1;
        for j:=0 to upr do
          if VarIsNull(Fields[j].Value) then
            Cells[j+1,i]:=''
          else
            Cells[j+1,i]:=Fields[j].Value;
        MoveNext;
      end;
      Close;
    end;
    mstLastRow:=ARow;
  end;
end;

procedure TfrmProject.strgrdMasterSetEditText(Sender: TObject; ACol,
  ARow: Integer; const Value: String);
begin
//a cell is edited
  mstEdits[ACol-1]:=true;
end;

procedure TfrmProject.btnCommitClick(Sender: TObject);
begin
  with con do
  begin
    CommitTrans;
    BeginTrans;
  end;
end;

procedure TfrmProject.btnRollbackClick(Sender: TObject);
begin
  with con do
  begin
    RollbackTrans;
    BeginTrans;
  end;
end;

procedure TfrmProject.btnAppendMstrClick(Sender: TObject);
begin
  strgrdMaster.RowCount:=strgrdMaster.RowCount+1;
end;

procedure TfrmProject.btnDeleteMstrClick(Sender: TObject);
var
  i,upr:integer;
  recAfctd:OleVariant;
begin
  with cmd do
  begin
    CommandText:='delete from project where proj_id=:proj_id';
    Parameters.Refresh;
    Parameters['proj_id'].Value:=proj_id;
    Execute(recAfctd,EmptyParam,0);
  end;
  with strgrdMaster do
  begin
    upr:=ColCount-1;
    for i:=1 to upr do
      Cells[i,mstLastRow]:='';
  end;
end;

end.





Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment













 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
E. DSpirito
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)