Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Relational Databases
Expertise: Beginner
Mar 19, 1997

Getting the List of Valid Aliases at Runtime

Question:
How can I load a list like a TComboBox Items property with the list of valid aliases at runtime?

Answer:

There are two ways to do this. The first is to use the current session's GetAliasNames method (the preferred and easiest); the second is to make some BDE calls. I'm presenting both of them here to demonstrate the flexibility of approaches you can take with Delphi.

Let's look at the first method.

Using the Current Session's GetAliasNames Method

If you put the DB unit in your uses section, every time your application launches, a default TSession component is created a long with it, called Session. A TSession component "provides global control over database connections for an application." By default, a TSession's list of valid databases contains those defined in your IDAPI32.CFG or IDAPI.CFG file. And you can add more databases to the list at run time. However, the databases you add are private to the TSession, so multiple TSessions can have different databases that point to different things on your local machine or your network. For a more detailed discussion of the TSession component, consult the help file.

To get a list of valid databases for the current session, all you need to do is call the GetAliasNames method. GetAliasNames takes a single argument of type Tstrings, clears the TStrings items, and loads into it all the valid database names available. For example, to load a TComboBox's Items property with the valid alias names for the current session, all you need to do is the following:

Session.GetAliasNames(ComboBox1.Items);

Once you do that, the valid database names will magically appear in the TComboBox's item list.

Using the BDE to Get Alias Names

I know I use a lot of BDE examples here at Ask The Delphi Pro, but I feel constant exposure to something reduces the fear of using it. The BDE is a beast that can be tamed with a little temerity and steadfastness.

With respect to getting alias names, it's a matter of making a few calls to the BDE. Coincidentally, the code I'll be listing is very similar to the code used by the TSession to get the database list. The difference is that TSession adds some calls to private procedures to get a snapshot of the environment before it gets the list. But the methods are essentially the same. Here's the code:

procedure LoadDataBaseList(List : TStrings);
var
  hCur    : hDbiCur;
  hDBDesc : DBDesc;
begin
  List.Clear;
  DbiInit(nil);
  Check(DbiOpenDatabaseList(hCur));

  while (DbiGetNextRecord(hCur, dbiNOLOCK, @hDBDesc, nil) = DBIERR_NONE) do
    List.Add(StrPas(hDBDesc.szName));

  Check(DbiCloseCursor(hCur));
  DbiExit;
end;

And here's the code for the TSession's GetAliasNames method:

procedure TSession.GetAliasNames(List: TStrings);
var
  Cursor: HDBICur;
  Desc: DBDesc;
begin
  List.BeginUpdate;
  try
    List.Clear;
    LockSession;
    try
      Check(DbiOpenDatabaseList(Cursor));
    finally
      UnlockSession;
    end;
    try
      while DbiGetNextRecord(Cursor, dbiNOLOCK, @Desc, nil) = 0 do
      begin
        OemToChar(Desc.szName, Desc.szName);
        List.Add(Desc.szName);
      end;
    finally
      DbiCloseCursor(Cursor);
    end;
  finally
    List.EndUpdate;
  end;
end;

Pretty close, right? So why bother to list the BDE method here? First of all, it gives me some room for discussion. Secondly, it shows that you have alternative means at your fingertips, as I mentioned above. So what's going on in the code (and I'm speaking in reference to my procedure)?

First, the list gets cleared and I initialize the BDE with a call to DBIInit, passing a NULL value for the database since all I want to do is open it up for access. Next, I call DbiOpenDatabaseList, which creates an in-memory table that contains information about each database (DBDesc structure) in a record format. I pass by reference a cursor variable so that I use DbiGetNextRecord to peruse the table, record-by-record. The most important parameter of DbiGetNextRecord is the one where I pass the address of the database descriptor variable. This variable is loaded with the current record's contents. From that, I grab its szName field value and copy into the list that was passed to LoadDatabaseList.

You see? No rocket science here. Play around with this and see what it can do for you.

DevX Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date