delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
500 Users Online NOW
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 (5)


Create a DBExpress-Connection at RuntimeGo to Max Kleiner's websiteComponent available for this articleFormat this article printer-friendly!Bookmark function is only available for registered users!
Product:
Delphi 6.x (or higher)
Category:
dbExpress
Skill Level:
Scoring:
Last Update:
08/20/2006
Search Keys:
delphi delphi3000 article borland vcl code-snippet dbExpress runtime connection SOA
Times Scored:
5
Visits:
11137
Uploader: Max Kleiner
Company: kleiner kommunikation
Reference: N/A
Component Download: http://www.softwareschule.ch/download/dpatterns3.zip
 
Question/Problem/Abstract:
If you have a Webservice or a nonvisual component, you can't put a TSQLConnection on a form so you have to call the connection at runtime
Answer:



The normal way for Delphi and Kylix is just to check dbExpress, put a TSQLConnection on a form then double-click the TSQLConnection to display the Connection Editor and set parameter values (database path, connection name etc.) to indicate the settings.

But in our example, all goes by runtime (path and login) with dbExpress we don't need an alias or the BDE either.


procedure TVCLScanner.PostUser(const Email, FirstName, LastName: WideString);
var
  Connection : TSQLConnection;
  DataSet    : TSQLDataSet;
begin
  Connection:= TSQLConnection.Create(nil);
  with Connection do begin
    ConnectionName:= 'VCLScanner';
    DriverName:= 'INTERBASE';
    LibraryName:= 'dbexpint.dll';
    VendorLib:= 'GDS32.DLL';
    GetDriverFunc:= 'getSQLDriverINTERBASE';
    Params.Add('User_Name=SYSDBA');
    Params.Add('Password=masterkey');
    Params.Add('Database=milo2:D:\frank\webservices\umlbank.gdb');
    LoginPrompt:= False;
    Open;
  end;
  DataSet:= TSQLDataSet.Create(nil);
  with DataSet do begin
    SQLConnection:= Connection;
    CommandText:= Format('INSERT INTO kings VALUES("%s","%s","%s")',
                             [Email,FirstN,LastN]);
    try
      ExecSQL;
    except
    end;
  end;
  Connection.Close;
  DataSet.Free;
  Connection.Free;
end;


Sending commands to the server
=================================
Another possibilities is to send commands like CreateTable to the Server. For TSQLConnection, Execute takes three parameters: a string that specifies a single SQL statement that you want to execute, a TParams object that supplies any parameter values for that statement, and a pointer that can receive a TCustomSQLDataSet that is created to return records.

Note: Execute can only execute one SQL statement at a time. It is not possible to execute multiple SQL statements with a single call to Execute, as you can with SQL scripting utilities. To execute more than one statement, call Execute repeatedly.

It is relatively easy to execute a statement that does not include any parameters. For example, the following code in our example executes a CREATE TABLE statement (DataDefinitionLanguage) without any parameters on a TSQLConnection component:


procedure createUserTable;
var
  Connection: TSQLConnection;
  SQLstmt: String;
begin
  Connection := TSQLConnection.Create(nil);
  with Connection do begin
    ConnectionName := 'VCLScanner';
    DriverName := 'INTERBASE';
    LibraryName := 'dbexpint.dll';
    VendorLib := 'GDS32.DLL';
    GetDriverFunc := 'getSQLDriverINTERBASE';
    Params.Add('User_Name=SYSDBA');
    Params.Add('Password=masterkey');
    with TWebModule1.create(NIL) do begin
        getFile_DataBasePath;
        Params.Add(dbPath);
        free;
    end;
    LoginPrompt := False;
    Connected := True;
    SQLstmt := 'CREATE TABLE NewMaxCusts ' +
    '( ' +
    '  CustNo INTEGER NOT NULL, ' +
    '  Company CHAR(40), ' +
    '  State CHAR(2), ' +
    '  PRIMARY KEY (CustNo) ' +
    ')';
   try
     Execute(SQLstmt, NIL, NIL);
   except
     raise
   end;
    Close;
    Free;
  end; //end Connection
end;



--------------------------------------------------------------
Update 1: dynamic dbExpress with Delphi10

const
StrDatabase2Dfr =
    'Database=APSN21:D:\kleiner2005\ekon9_10\soa_vcl\umlbank2.gdb';
StrInsertSQLStat = 'insert into KINGS values ("%s", "%s", "%s")';


procedure TVCLScanner.PostUser(const Email, FirstName, LastName: WideString);
var
  SQLConnection1: TSQLConnection;
  DataSet: TSQLDataSet;
begin
  //webModule1.SQLConnection1.Open;
  SQLConnection1:= TSQLConnection.Create(NIL);
  with SQLConnection1 do begin
    ConnectionName := 'VCLScanner';
    DriverName := 'INTERBASE';
    //LibraryName := 'dbexpint.dll' in D6;
    LibraryName:= 'dbxint30.dll';
    VendorLib:= 'GDS32.DLL';
    GetDriverFunc:= 'getSQLDriverINTERBASE';
    Params.Add('User_Name=SYSDBA');
    Params.Add('Password=masterkey');
    //Params.Add('Database=myserver:X:\vclscanner.gdb');
    Params.Add(StrDatabase3Dfr);
    LoginPrompt:= False;
    Open;
  end;
  DataSet:= TSQLDataSet.Create(nil);
  with DataSet do begin
    SQLConnection:= SQLConnection1;
    CommandText:= Format(StrInsertSQLStat,
                             [Email,FirstName,LastName]);
    try
      ExecSQL;
    //silent cause of CGI Webscript
    except
    end;
  end;
  SQLConnection1.Close;
  DataSet.Free;
  SQLConnection1.Free;
end;





Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment
CREATE DATABASE....
    Osman AKDEMIR (Aug 29 2002 5:27PM)

Please help me....How can i create A database in code with DbExpress..wihch is MS SQL or INTERBASE Database...
Respond

RE: CREATE DATABASE....
Max Kleiner (Sep 2 2002 10:51AM)

In the meantime I wrote an example to create a Table at runtime (see article) but it remains to be seen creating a whole database with TSQLConnection. Execute takes three parameters: a string that specifies a single SQL statement that you want to execute, a TParams, so I'll ckeck it out with TParams to create a database.
Respond

RE: CREATE DATABASE....
Bilgehan Evren (Feb 13 2003 10:53PM)

merhaba delphi3000 de sorunu gordum.

sen bir databse i dbexpress ile olusturamazsin. Onu SQL kullanarak olusturursun. SQLconnection ile MYSQL veya ORACLE'ye baglanip SQL sorgusunu gondererek bir database olusturabilirsin. yhepsi bu..

in English

you send a SQL query to Database. only this make.

Respond

RE: RE: CREATE DATABASE....
Ence Yuhandi (Jun 23 2005 7:20AM)

source code connection dbxpress from mysql
Respond

RE: RE: RE: CREATE DATABASE....
Max Kleiner (Jun 28 2005 11:46AM)

there's an article about dbExpress and MySQL on
http://www.thedelphimagazine.com/samples/1540/1540.htm
Respond














 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
M. Kleiner
 
   














 







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