unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, Forms, Controls, StdCtrls, ExtCtrls,
ODBCconn, SQLDB, DB, DBGrids, DBCtrls;
type
{ TForm1 }
TForm1 = class(TForm)
Connection: TODBCConnection;
Transaction: TSQLTransaction;
Query: TSQLQuery;
DataSource: TDatasource;
DBGrid2: TDBGrid;
DBNavigator2: TDBNavigator;
ListBox1: TListBox;
Splitter2: TSplitter;
procedure FormCreate(Sender: TObject);
procedure ListBox1Click(Sender: TObject);
private
public
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.ListBox1Click(Sender: TObject);
var
tblName: string;
SQL: String;
begin
if Listbox1.ItemIndex = -1 then
Exit;
tblName := Listbox1.Items[Listbox1.ItemIndex];
SQL := 'SELECT * FROM ' + tblName;
Query.Close;
Query.SQL.Text := SQL;
Query.Open;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
dbName: String;
begin
dbName := Application.Location + 'data.xls';
// Connection
Connection.Driver:= 'Microsoft Excel Driver (*.xls)';
Connection.Params.Text := 'DBQ=' + dbName;
Connection.Params.Add('DriverID=790');
Connection.Params.Add('ReadOnly=1');
Connection.Params.Add('AutoCommit=1');
Connection.Params.Add('FirstRowHasNames=1');
Connection.KeepConnection := True;
Connection.Open;
// Transaction
Transaction.DataBase := Connection;
Transaction.Action := caCommit;
Transaction.Active := False;
Transaction.StartTransaction;
// Get table names
Connection.GetTableNames(Listbox1.Items);
// Important settings:
Query.Database := Connection;
Query.Transaction := Transaction;
Query.UsePrimaryKeyAsKey := False;
end;
end.