How to: Set the File Mode when Opening a Database (Programmatically)

In this topic, you will learn how to set the file mode when you open a Microsoft SQL Server Compact 4.0 database. To set the mode in which the database is opened, you use the mode property in the connection string. The following table defines the valid values for the file mode:

Value

Definition

Read Write

(default mode)

Permits multiple processes to open and modify the database.

Read Only

Lets you open a read-only copy of the database.

Exclusive

Does not permit other processes to open or modify the database.

Shared Read

Permits other processes to read, but not modify, the database while you have it open.

Note

If you are opening a database that is located on a file share, use the Exclusive file mode.

To open the database exclusively

  • When you access the SQL Server Compact database, set the mode property in the connection string to Read/Write, Read Only, Exclusive, or Shared Read. For example, you can set the ConnectionString property of a SqlCeEngine object to ensure an exclusive lock on the database until the SqlCeEngine object is destroyed.

    SqlCeEngine eng = new SqlCeEngine();
    eng.LocalConnectionString="Data Source=MyDatabase.sdf; " + 
       "Mode = Exclusive";
    

See Also

Concepts

Maintaining Databases (SQL Server Compact)

Accessing and Modifying Databases (SQL Server Compact)

Other Resources

Working with Databases (SQL Server Compact)