Partilhar via


Controlling Access to Data

Since you access data in files, effective data management begins with control over the environment of these files. You must choose how to access the data and how and when to limit that access.

Accessing Data

In a shared environment, you can access data in two ways: from exclusive files or from shared files. If you open a table for shared access, other users also have access to the file. If you open a table for exclusive access, no other user can read or write to that file. Because exclusive use defeats many of the benefits of sharing data on a network, it should be used sparingly.

Using a Table with Exclusive Access

The most restrictive way to open a file is to open it exclusively. When you open a table through the interface, it opens for exclusive use by default. You can also explicitly open a table for exclusive use by using Visual FoxPro commands.

To open a table for exclusive use

  • Type the following commands in the Command window:

    SET EXCLUSIVE ON
    USE cMyTable
    

    -or-

  • Type the following command in the Command window:

    USE cMyTable EXCLUSIVE
    

The following commands require you to open a table for exclusive use:

  • ALTER TABLE
  • INDEX when creating, adding, or deleting a compound index tag.
  • INSERT [BLANK]
  • MODIFY STRUCTURE To use this command to change a table structure, you must open the table exclusively. You can, however, use this command in read-only mode when you open the table for shared use.
  • PACK
  • REINDEX
  • ZAP

Visual FoxPro returns the error, "Exclusive open of file is required," if you try to execute one of these commands on a shared table.

You can restrict access to a table by using the FLOCK( ) function. If you use FLOCK( ) to lock the table, other users cannot write to the table but they can read it.

Using a Table with Shared Access

When you open a table for shared use, more than one workstation can use the same table at the same time. When you open a table through the interface, you can override the default ON setting for SET EXCLUSIVE. You can explicitly open a table for shared use by using Visual FoxPro commands.

To open a table for shared use

  • Type the following commands in the Command window:

    SET EXCLUSIVE OFF
    USE cMyTable
    

    -or-

  • Type the following command in the Command window:

    USE cMyTable SHARED
    

When you add or change data in a shared table, you must first lock the affected record or the entire table. You can lock a record or a table opened for shared use in the following ways:

  • Use a command that performs an automatic record or table lock.
  • Manually lock one or more records or an entire table with the record and table locking functions.
  • Initiate buffering with the CURSORSETPROP( ) function.

Associated memo and index files always open with the same share status as their table.

If your application uses a table for lookup purposes only and all users of the application access it, then you can improve performance by marking the table as read-only.

See Also

Programming for Shared Access | Locking Data | ALTER TABLE | INDEX | INSERT [BLANK] | MODIFY STRUCTURE | Using Data Sessions | Buffering Data