SqlCeEngine.Compact Method

Reclaims wasted space in the SQL Server Compact 3.5 SP1 database by creating a new database file from the existing file. This method is also used to change the collating order, encryption, or password settings of the database.

Namespace: System.Data.SqlServerCe
Assembly: System.Data.SqlServerCe (in system.data.sqlserverce.dll)

Syntax

'Declaration
Public Sub Compact ( _
    connectionString As String _
)
public void Compact (
    string connectionString
)
public:
void Compact (
    String^ connectionString
)
public void Compact (
    String connectionString
)
public function Compact (
    connectionString : String
)

Parameters

  • connectionString
    The connection string to the destination database.

Remarks

The connection string specifies a connection to the destination database that will be created by this method.

An exception is thrown if the specified database already exists or if another file with the same name already exists. .

If you pass an empty string for the connection string, the new database file overwrites the old database file and maintains the same name.

Important

Abnormal termination of an application before the Compact operation is completed leaves a temporary database file in the system. This file is named <filename>pack.sdf and must be manually removed.

Example

The following example demonstrates how to compact a SQL Server Compact 3.5 SP1 database.

Dim engine As New SqlCeEngine("Data Source = AdventureWorks.sdf")

' Specify null destination connection string for in-place compaction
'
engine.Compact(Nothing)

' Specify connection string for new database options; The following 
' tokens are valid:
'      - Password
'      - LCID
'      - Encrypt
' 
' All other SqlCeConnection.ConnectionString tokens are ignored
'
engine.Compact("Data Source=; Password =a@3!7f$dQ;")
SqlCeEngine engine = new SqlCeEngine("Data Source = AdventureWorks.sdf");

// Specify null destination connection string for in-place compaction
//
engine.Compact(null);

// Specify connection string for new database options; The following 
// tokens are valid:
//      - Password
//      - LCID
//      - Encrypt
// 
// All other SqlCeConnection.ConnectionString tokens are ignored
//
engine.Compact("Data Source=; Password =a@3!7f$dQ;");

Thread Safety

Any public static (Shared in Microsoft Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Platforms

Development Platforms

Windows Vista, Windows Mobile 5.0, Windows XP Professional with Service Pack 2 (SP2), Windows Server 2003, Windows Mobile 2003 for Pocket PC, Windows CE 5.0
Version Information
.NET Framework and NET Compact Framework
Supported in 3.5
.NET Framework
Supported in 3.0
.NET Compact Framework and .Net Framework
Supported in 2.0

See Also

Reference

SqlCeEngine Class
SqlCeEngine Members
System.Data.SqlServerCe Namespace