Solved

dynamic connection string

Posted on 2004-09-22
7
334 Views
Last Modified: 2012-05-05
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+';DefaultDir=C:;DriverId=25;FIL=MS Access;MaxBufferSize=2048;PageTimeout=5;UID=admin;";Initial 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

 



0
Comment
Question by:BaTy_GiRl
7 Comments
 
LVL 13

Expert Comment

by:BlackTigerX
ID: 12123998
don't put that code in the OnCreate event of your form, but instead put it in the "Start" button OnClick event of your program, where it actually goes and uses that database
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(ChangeFileExt(Application.ExeName, '.ini')) do
  try
    dbPath:=ReadString('DB', 'DBPath', 'd:\db_pd.mdb');
  finally
    Free
  end
0
 
LVL 13

Accepted Solution

by:
BlackTigerX earned 50 total points
ID: 12124057
if you don't have a "Start" kind of thing, then if the other forms have datasets pointing to This connection, check to see if the connection if Active before opening any of the other forms where requested by the user, if is not active, then try to activate it at that point

The thing is, if you want to use dinamyc connection string, it doesn't make much difference if you only create it in the OnCreate event of your form, is not really very dinamyc, is is "TRULY" dinamic, if you create it ONLY when you need it
0
 

Author Comment

by:BaTy_GiRl
ID: 12124137
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
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 

Author Comment

by:BaTy_GiRl
ID: 12124215
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


0
 
LVL 12

Expert Comment

by:Ivanov_G
ID: 12128320
if FileExists(dbpath) then ...
0
 
LVL 22

Expert Comment

by:Ferruccio Accalai
ID: 12141071
--> 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.ADOConnection1BeforeConnect(Sender: 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+';DefaultDir=C:;DriverId=25;FIL=MS Access;MaxBufferSize=2048;PageTimeout=5;UID=admin;";Initial 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 ;-)
0
 

Author Comment

by:BaTy_GiRl
ID: 12145932
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

0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Help on project with Soap 10 54
Create a path if not exists 7 77
Convert MS Word document to a PDF file 9 80
Firemonkey allowing RTL on android 6 32
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question