Icon Opening Tables and Views

Opening tables and views can be accomplished through the Open method of the TEDBTable component, or by setting the Active property to True. Before opening a table or view, however, you must first specify the source database of the table or view and the table or view name. The source database of the table or view is specified in the DatabaseName property of the TEDBTable component, and the table or view name is specified in the TableName property.

Setting the DatabaseName Property
You may specify the DatabaseName property using two different methods:

1) The first method is to set the DatabaseName property of the TEDBTable component to the DatabaseName property of an existing TEDBDatabase component within the application. In this case the actual source database being used will come from the Database property. The following example shows how to use the DatabaseName property to point to an existing TEDBDatabase component for the source database:

begin
   with MyDatabase do
      begin
      DatabaseName:='AccountingDB';
      Database:='Accounting';
      Connected:=True;
      end;
   with MyTable do
      begin
      DatabaseName:='AccountingDB';
      TableName:='ledger';
      Active:=True;
      end;
end;

Information The above example does not assign a value to the SessionName property of either the TEDBDatabase or TEDBTable component because leaving this property blank for both components means that they will use the default session that is automatically created by ElevateDB when the engine is initialized. This session is, by default, a local, not remote, session named "Default" or "". Please see the Starting Sessions topic for more information.

Another useful feature is using the BeforeConnect event of the TEDBDatabase component to dynamically set the Directory or RemoteDatabase property before the TEDBDatabase component attempts to connect to the database. This is especially important when you have the Connected property for the TEDBDatabase component set to True at design-time during application development and wish to change the Directory or RemoteDatabase property before the connection is attempted when the application is run.

2) The second method is to enter the name of an existing database directly into the DatabaseName property. In this case a temporary database component will be automatically created, if needed, for the database specified and automatically destroyed when no longer needed. The following example shows how to use the DatabaseName property to point directly to the desired database without referring to a TEDBDatabase component:

begin
   with MySession do
      begin
      SessionName:='Remote';
      SessionType:=stRemote;
      RemoteAddress:='192.168.0.2';
      Active:=True;           
      end;
   with MyTable do
      begin
      SessionName:='Remote';
      DatabaseName:='Accounting';
      TableName:='ledger';
      Active:=True;
      end;
end;

Exclusive and ReadOnly Open Modes
In the above two examples we have left the Exclusive and ReadOnly properties of the TEDBTable component at their default value of False. However, you can use these two properties to control how the table or view is opened and how that open affects the ability of other sessions and users to open the same table or view.

When the Exclusive property is set to True, the table or view specified in the TableName property will be opened exclusively when the Open method is called or the Active property is set to True. This means that neither the current session nor any other session or user may open this table or view again without causing an EEDBError exception. It also means that the table or view open will fail if anyone else has the table or view opened either shared (Exclusive=False) or exclusively (Exclusive=True). The error code raised when a table open fails due to access problems is 300 (EDB_ERROR_LOCK). The following example shows how to trap for such an exception using a try..except block (Delphi and Lazarus) or try..catch block (C++) and display an appropriate error message to the user:

begin
   with MySession do
      begin
      SessionName:='Remote';
      SessionType:=stRemote;
      RemoteAddress:='192.168.0.2';
      Active:=True;           
      end;
   with MyDatabase do
      begin
      SessionName:='Remote';
      DatabaseName:='AccountingData';
      Database:='Accounting';
      Connected:=True;
      end;
   with MyTable do
      begin
      SessionName:='Remote';
      { We're using a database component for the source
        database, so we use the same value as the DatabaseName
        property for the TEDBDatabase component above, not
        the same value as the Database property, which
        is the name of the actual database }
      DatabaseName:='AccountingData';
      TableName:='ledger';
      Exclusive:=True;
      ReadOnly:=False;
      try
         Open;
      except
         on E: Exception do
            begin
            if (E is EDatabaseError) and
               (E is EEDBError) then
               begin
               if (EEDBError(E).ErrorCode=EDB_ERROR_LOCK) then
                  ShowMessage('Cannot open table '+TableName+
                              ', another user has the table '+
                              'open already')
               else
                  ShowMessage('Unknown or unexpected database '+
                              'engine error # '+
                              IntToStr(EEDBError(E).ErrorCode));
               end
            else
               ShowMessage('Unknown or unexpected error has occurred');
            end;
      end;
      end;
end;

Information Regardless of whether you are trying to open a table or view exclusively, you can still receive this exception if another user or application has opened the table or view exclusively.

When the ReadOnly property is set to True, the table or view specified in the TableName property will be opened read-only when the Open method is called or the Active property is set to True. This means that the TEDBTable component will not be able to modify the contents of the table or view until the table is closed and re-opened with write access (ReadOnly=False). If any of the physical files that make up a table are marked read-only at the operating system level (such as is the case with CD-ROMs) then ElevateDB automatically detects this condition and sets the ReadOnly property to True. ElevateDB is also able to do extensive read buffering on any table that is marked read-only at the operating system level, so if your application is only requiring read-only access then it would provide a big performance boost to mark the tables as read-only at the operating system level. Finally, if security permissions for any of the physical files that make up the table prevent ElevateDB from opening the table with write access, then ElevateDB will also automatically detect this condition and set the ReadOnly property to True.

Updateable Views
Views behave just like tables in most cases. However, views can only be updated if they are actually flagged as updateable by ElevateDB when they are created. You can find out if a view is updateable by querying the Views Table in the Information Schema for the current database. For a view to be flagged as updateable, it must adhere to the requirements of a query that can generate a sensitive result set cursor. Please see the Result Set Cursor Sensitivity topic for more information. If a view is not updateable, then it will always have its ReadOnly property set to True when it is opened.
Image