Entry
Delphi: Database: Create: Using SQL how to dynamically create, using alias, database and table: BDE?
Aug 24th, 2003 13:43
Knud van Eeden,
-----------------------------------------------------------------------
--- Knud van Eeden - 08 August 2003 - 02:41 am ------------------------
Delphi: Database: Create: Using SQL how to dynamically create, using
alias, database and table: BDE?
Language: Computer: Borland: Delphi: Database: How to create
dynamically, using SQL, a database table with an alias in
Delphi in your source code?
1. Put the following components on your form:
1. button (from palette 'standard')
2. datasource (from palette 'dataaccess')
3. table (from palette 'BDE')
4. database (from palette 'BDE')
5. query (from palette 'BDE')
6. dbgrid (from palette 'datacontrols')
2. Then compile this
(works OK!, and creates also this alias in the
BDE engine
(use e.g. menu 'Database'->'Explorer' to check this,
you should see your alias (e.g. 'MyNewAlias') added there)
---
Compiled successfully in Delphi 7, on Microsoft Windows XP
Professional.
---
--- cut here ---------------------------------------------------------
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls,
Forms,
Dialogs, DB, DBTables, StdCtrls, Grids, DBGrids;
type
TForm1 = class(TForm)
Button1: TButton;
DBGrid1: TDBGrid;
DataSource1: TDataSource;
Database1: TDatabase;
Query1: TQuery;
Table1: TTable;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click( Sender: TObject );
begin
Database1.DatabaseName := 'MyNewAlias';
Database1.DriverName := 'STANDARD';
Database1.Params.Clear;
Database1.Params.Add( 'PATH=C:\TEMP' );
Table1.DatabaseName:= 'MyNewAlias';
Table1.TableName := 'CUSTOMER';
//
// this will create a table in the directory as indicated between ""
after CREATE table
//
if not Table1.Exists then begin
ShowMessage( 'Table could not be found or does not exist: a new
table will be created' );
Query1.Close;
Query1.SQL.Clear;
ShowMessage( 'CREATE TABLE "C:\TEMP\CUSTOMER.DB" ( agentnameS VARCHAR
( 50 ), casetypeS VARCHAR( 10 ), programnameS VARCHAR( 50 ),
casenumberS VARCHAR( 10 ), casedateS VARCHAR( 10 ), casetimeS VARCHAR(
15 ) );' );
Query1.SQL.Text := 'CREATE TABLE "C:\TEMP\CUSTOMER.DB" ( agentnameS
VARCHAR( 50 ), casetypeS VARCHAR( 10 ), programnameS VARCHAR( 50 ),
casenumberS VARCHAR( 10 ), casedateS VARCHAR( 10 ), casetimeS VARCHAR(
15 ) );';
Query1.ExecSQL;
//
Session.AddStandardAlias('MyNewAlias', 'C:\TEMP', 'PARADOX' );
Session.SaveConfigFile; // permanently store this changes in the BDE
(check via menu 'Database'->'Explorer')
end;
//
// Show the table in the grid
//
Table1.Active := True;
DataSource1.DataSet:= Table1;
DBGrid1.DataSource:= DataSource1;
end;
end.
--- cut here ---------------------------------------------------------
---
See also:
http://www.faqts.com/knowledge_base/view.phtml/aid/23858/fid/175
---
To create a database alias using the Borland Database Desktop: see
also:
http://www.faqts.com/knowledge_base/view.phtml/aid/23861/fid/175
----------------------------------------------------------------------