How do I change the path of my database when running the program?
I'm starting to program with Delphi XE8and using Firebird 2.5.
I managed to implement my database and it is working all right.
The problem I'm facing now is that as I switch from computers to programming (home, college, etc), I have to change the path where my database file is located, compile and only then can I run the program.
I want to change the configuration of my TSQL Connection at runtime and not having to compile the program every time I switch computer.
In this case, create a form with the program settings or, before the program initializes, request the database path.
In the image is highlighted the property I want to modify.
I tried with this code:
procedure TDMDados.ConexaoBeforeConnect(Sender: TObject);
begin
Conexao.Params:= 'Caminho do arquivo';
end;
The problem is that, as I have already configured this connection, this parameter is already set.
Some connection parameters:
DriverName=Firebird
DriverUnit=Data.DBXFirebird
DriverPackageLoader=TDBXDynalinkDriverLoader,DbxCommonDriver220.bpl
DriverAssemblyLoader=Borland.Data.TDBXDynalinkDriverLoader,Borland.Data.DbxCommonDriver,Version=22.0.0.0,Culture=neutral,PublicKeyToken=91d62ebb5b0d1b1b
MetaDataPackageLoader=TDBXFirebirdMetaDataCommandFactory,DbxFirebirdDriver220.bpl
MetaDataAssemblyLoader=Borland.Data.TDBXFirebirdMetaDataCommandFactory,Borland.Data.DbxFirebirdDriver,Version=22.0.0.0,Culture=neutral,PublicKeyToken=91d62ebb5b0d1b1b
LibraryName=dbxfb.dll
LibraryNameOsx=libsqlfb.dylib
VendorLib=fbclient.dll
VendorLibWin64=fbclient.dll
VendorLibOsx=/Library/Frameworks/Firebird.framework/Firebird
Database=C:\Users\Johnny\Dropbox\1.MESTRADO\10.DISSERTAÇÃO\PROGRAMA DELPHI\Banco de dados\bd.fdb
User_Name=sysdba
Password=masterkey
Role=RoleName
[...]
My intention is to change this parameter of the connection at runtime:
Database=C:\Users\Johnny\Dropbox\1.MESTRADO\10.DISSERTAÇÃO\PROGRAMA DELPHI\Banco de dados\bd.fdb
"- I still haven't been able to try to do this because I don't know how to start. I know how to change the parameters of the connection before compiling ".
1 answers
After getting a lot here is the solution I got, it met my need satisfactorily.
Before the program creates the form1 the user is asked to select the file from the database, then the program manages to get the file path. I created a function (SelectBDpath) to get the file path and sequentially called the function and applied it to a variable which will be used in my DataModule.
function SelectBDpath() : string ;
var
selectedFile: string;
dlg: TOpenDialog;
begin
selectedFile := '';
dlg := TOpenDialog.Create(nil);
try
dlg.InitialDir := GetCurrentDir;
dlg.Filter := 'Firebird DB Files|*.FDB';
if dlg.Execute
then
SelectBDpath := dlg.FileName;
finally
dlg.Free;
end;
end;
//Evento *OnCreate*
procedure TForm1.FormCreate(Sender: TObject);
begin
BDpath:=SelectBDpath();
DMDados.Conexao.Open;
end;
In sequence my DataModule , before the connection occurs, "pulls" the file path (bdpath) of Form1 (for this it is necessary to put Unit corresponding in uses of DataModule ) and applies in the parameters of the connection of my database, as code below:
procedure TDMDados.ConexaoBeforeConnect(Sender: TObject);
begin
Conexao.Params.Values['Database']:= BDpath;
end;
This way every time I run the program it will ask to select the file from my database and apply on the parameters of the connection.