Database Creation and Connection

   

The TIBDatabase component is used to establish connections to InterBase databases using either the local host, TCP/IP, NetBEUI, or SPX protocol. However, parameters for connecting to a database are different from those used for creating a database. The server and the database to be connected to are specified in the DatabaseName property. C++Builder's help file has information on formatting the DatabaseName property for different protocols. Listing 10.4 shows how to use an enum type and a function to create an appropriate string for each protocol.

Listing 10.4 The CreateConnectionString Function
[View full width]
 enum ConnectionType {ctLocal,ctTCPIP,ctNetBEUI,ctSPX};  AnsiString __fastcall TdmMain::CreateConnectionString(AnsiString Server, AnsiString graphics/ccc.gif FileName, ConnectionType CType)  {     AnsiString ConnectionString = "";     switch (CType)     {     case ctLocal : ConnectionString = FileName;break;     case ctTCPIP : ConnectionString.sprintf("%s:%s",Server,FileName);break;     case ctNetBEUI : ConnectionString.sprintf("\%s\%s",Server,FileName);break;     case ctSPX : ConnectionString.sprintf ("%s@%s",Server,FileName);break;     }     return ConnectionString;  } 

CAUTION

In certain instances, it might be necessary to use a server name in place of an actual IP address when connecting to InterBase using TCP/IP connections. For example, ibserver:c:\bugger.gdb will work, but 192.168.0.9:c:\bugger.gdb sometimes won't, even if the ibserver machine's address is 192.168.0.9 . This is frequently seen in machines that have a version of Winsock earlier than version 2.0.

To overcome this anomaly, add an entry in the hosts file in Windows 95/98 (located under the Windows folder) or the hosts file in Windows NT/2000 (located under %SystemRoot%\System32\Drivers\Etc ).

For example, if you want to connect to a server at 192.168.0.11 , add the following line:

 192.168.0.11 theserver  The following hosts file shows how this might look after the addition.  # Copyright (c) 1993-1999 Microsoft Corp.  #  # This is a sample HOSTS file used by Microsoft TCP/IP for Windows.  #  # This file contains the mappings of IP addresses to host names. Each  # entry should be kept on an individual line. The IP address should  # be placed in the first column followed by the corresponding host name.  # The IP address and the host name should be separated by at least one  # space.  #  # Additionally, comments (such as these) may be inserted on individual  # lines or following the machine name denoted by a '#' symbol.  #  # For example:  #  #      102.54.94.97     rhino.acme.com          # source server  #       38.25.63.10     x.acme.com              # x client host  127.0.0.1       localhost  192.168.0.11 theserver 

After saving the file, either restart the system to effect the changes or run the following command from the DOS prompt:

 nbtstat R 

Note that the -R parameter must be capitalized. We can then access the server at 192.168.0.11 by using theserver:c:\bugger.gdb .

Also, when coding paths in C++ functions don't forget that the \ is an escape character used to indicate other characters and that to show an actual backslash requires two backslashes in a row.


Having assigned the DatabaseName property, we must supply the username and password. This can be done in one of two different ways. The first is to set the LoginPrompt property to true, which will open a window asking the user for the information. The second is to assign the username and password in the Params property. The following code shows a sample set of parameters for logging in:

 user_name=SYSDBA  password=masterkey 

Keep in mind that to avoid the login dialog is necessary to set the login prompt property to false. Also, under C++Builder 6 to use the login prompt dialogue you need to include a special unit in your project. The #include is DBLogDlg.hpp , which should appear in your .cpp file. You also need to add DBLogDlg.pas from your C++ Builder installation Source\VCL directory, where project will not successfully link.

After these items are established, setting the Connected property to true will make the connection to the database.

NOTE

If you have problems generating the proper connection string parameters at runtime, the TIBDatabase component has an editor that will generate examples for you to use.


Although InterBase Express can create and drop databases, it does not currently support SQL scripts. Therefore, you are unable to run an SQL script against a newly created database to create the necessary tables, triggers, and so on. An empty database of the correct format normally would be created and shipped and with a product such as this, however, it is possible to repeatedly insert and execute appropriate CREATE TABLE and ALTER TABLE statements through a TIBQuery component using the ExecSQL() function.


   
Top


C++ Builder Developers Guide
C++Builder 5 Developers Guide
ISBN: 0672319721
EAN: 2147483647
Year: 2002
Pages: 253

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net