0

I want to change my database path programmatically. I have a folder with several tables, and my users should be able to change from one database to another. For example: active db: D:\app_db\db1 could change to D:\app_db\db2

 void change_db(std::string dir)
 { 
  char c[MAX_PATH]={0};  
  TStringList *list= new TStringList();
  list->Clear();
  sprintf(c,"PATH=%s",dir.c_str());
  list->Add(c);
  Session->ModifyAlias(dbalias,list);
  delete list;
  Table1->DatabaseName = dbalias;
}      

When I run the routine above the tables still contain the old path!

Table1->Database->Directory = c;

I set the Directory to new Directory, but my application still uses the old tables.

What is wrong here?

Thanks

1 Answers1

0

For simple Paradox access, I recommend you use theTDatabase component together with TTable or TQuery. Don't use aliases.

Set TDatabase.DatabaseName to anything you want. You use this name to link TTable and TQuery components to the TDatabase component. Their corresponding DatabaseName properties should be set to the same name as the TDatabase component.

Set TDatabase.DriverName to STANDARD.

Make sure TDatabase.Connected is set to false.

To set the path to the database use the TDatabase.Params stringlist.

First clear the list by calling the Clear method on Params, then set the path by calling the Add method.

If you need multiuser access then you also need to set a netdir on the embedded TDatabase session component.

This is what it would look like in Delphi:

  MyDatabase.Close;
  MyDatabase.Params.Clear;
  MyDatabase.Params.Add('PATH=' + PathToYourDatabase);
  // NetFileDir can be the same as the database, but I recommend a different folder.
  // Only needed for multiuser access. All users must use the same folder.
  MyDatabase.Session.NetFileDir := PathToYourNetFileDir;
  MyDatabase.Open;

You don't need to add a TSession component if you only need one connection to the database and only access the database from the main UI thread. TDatabase automatically creates a default session component in the Session property.

Anders E. Andersen
  • 1,635
  • 2
  • 14
  • 20