Mega Code Archive

 
Categories / Delphi / Ide Indy
 

Create a DBExpress Connection at Runtime

Title: Create a DBExpress-Connection at Runtime Question: 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;