0

我想以编程方式更改我的数据库路径。我有一个包含多个表的文件夹,我的用户应该能够从一个数据库更改到另一个数据库。例如:active db: D:\app_db\db1 可以更改为 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;
}      

当我运行表格上方的例程时,仍然包含旧路径!

Table1->Database->Directory = c;

我将目录设置为新目录,但我的应用程序仍使用旧表。

这里有什么问题?

谢谢

4

1 回答 1

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.

于 2014-07-25T10:35:22.233 回答