BaTy_GiRl
asked on
dynamic connection string
hello friends mine, today i have a simple question....
im working with a control TADOConnection, since now i was working with a static connectionstring,
but now, my aplication requires to be able to change the path from the application...just for make it configurable :) ....
so i have this code
procedure Tfrm_tmp.FormCreate(Sender : TObject);
begin
try
dbpath:='D:\bd_pd.mdb';
conexion.ConnectionString: ='Provider =MSDASQL.1 ;Persist Security Info=False;Extended Properties="DSN=MS Access Database;DBQ='+dbpath+';De faultDir=C :;DriverId =25;FIL=MS Access;MaxBufferSize=2048; PageTimeou t=5;UID=ad min;";Init ial Catalog='+dbpath;
except
end;
end;
Where frm_tmp is the main form ....
the wrong is when i try to catch the error when the path isnt found... because a lot of forms depends of the connectionstring and those forms were created at the beging of application
im working with a control TADOConnection, since now i was working with a static connectionstring,
but now, my aplication requires to be able to change the path from the application...just for make it configurable :) ....
so i have this code
procedure Tfrm_tmp.FormCreate(Sender
begin
try
dbpath:='D:\bd_pd.mdb';
conexion.ConnectionString:
except
end;
end;
Where frm_tmp is the main form ....
the wrong is when i try to catch the error when the path isnt found... because a lot of forms depends of the connectionstring and those forms were created at the beging of application
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
do you mean in the second option that i use a file to specify the path?
i`m getting that information from windows registry and i thing is surely
about the first option
>put it in the "Start" button OnClick event of your program, where it actually goes and uses that database
well the problem is that i have like 35 forms calling anothers and almost all of them uses the database
:( thanks
i`m getting that information from windows registry and i thing is surely
about the first option
>put it in the "Start" button OnClick event of your program, where it actually goes and uses that database
well the problem is that i have like 35 forms calling anothers and almost all of them uses the database
:( thanks
ASKER
im agree with you at this point
> OnCreate event of your form, is not really very dinamyc, is is "TRULY" dinamic, if you create it ONLY when you need it
let me try with that idea....and i will tell you what did happen= )
thanks
> OnCreate event of your form, is not really very dinamyc, is is "TRULY" dinamic, if you create it ONLY when you need it
let me try with that idea....and i will tell you what did happen= )
thanks
if FileExists(dbpath) then ...
--> well the problem is that i have like 35 forms calling anothers and almost all of them uses the database
In this case i'd use simply the OnBeforeConnect event and check there if dbPath exists
This way you don't need to put a check somewhere, 'cause every time any form try to open the connection the event is fired.
so something like
procedure TForm1.ADOConnection1Befor eConnect(S ender: TObject);
begin
If not FileExists(dbpath) then
begin
// SHowMessage('Path Not Found'); or you can use an opendialog or something else to force an existent path
//Abort; If you want to stop the connection
end;
conexion.ConnectionString: ='Provider =MSDASQL.1 ;Persist Security Info=False;Extended Properties="DSN=MS Access Database;DBQ='+dbpath+';De faultDir=C :;DriverId =25;FIL=MS Access;MaxBufferSize=2048; PageTimeou t=5;UID=ad min;";Init ial Catalog='+dbpath
end;
And i repeat this event is fired due to a call to conexion.connected := true or conexion.open, so from whereever is needed
F68 ;-)
In this case i'd use simply the OnBeforeConnect event and check there if dbPath exists
This way you don't need to put a check somewhere, 'cause every time any form try to open the connection the event is fired.
so something like
procedure TForm1.ADOConnection1Befor
begin
If not FileExists(dbpath) then
begin
// SHowMessage('Path Not Found'); or you can use an opendialog or something else to force an existent path
//Abort; If you want to stop the connection
end;
conexion.ConnectionString:
end;
And i repeat this event is fired due to a call to conexion.connected := true or conexion.open, so from whereever is needed
F68 ;-)
ASKER
it sems like very good idea Ferruccio68, i haven´t tried yet .....
but my program is working as BlackTigerX told me, the connection string is in all forms
but i alwas tought that it was another way.
so thanks for your help me im going to accept the two answers
=) greetings
but my program is working as BlackTigerX told me, the connection string is in all forms
but i alwas tought that it was another way.
so thanks for your help me im going to accept the two answers
=) greetings
by doing it this way, you can change the configuration without having to close the program, just Stop the program, fix your configuration, Start, and you are ready to go, if it fails, then the "start" process fails, and you don't let the program continue
the other thing is, don't hardCode values, read them at least from an .ini file
for that, you can use something like this (put IniFiles in your uses clause)
with TIniFile.Create(ChangeFile
try
dbPath:=ReadString('DB', 'DBPath', 'd:\db_pd.mdb');
finally
Free
end