如何:壓縮資料庫 (以程式設計的方式)
在此主題中,您將瞭解如何使用 SqlServerCe.Engine 物件的 Compact 方法,來壓縮 Microsoft SQL Server 2005 Compact Edition (SQL Server Compact Edition) 資料庫。如需使用 SqlServerCe 命名空間的詳細資訊,請參閱 SqlServerCe 命名空間的參考說明文件。
壓縮 SQL Server Compact Edition 資料庫
建立 Engine 物件,並傳遞到您要壓縮之現有資料庫的連接字串中。
SqlCeEngine engine = new SqlCeEngine("Data Source = AdWks.sdf");
呼叫 Compact 方法。當呼叫 Compact 方法時,也可以指定新的資料庫屬性,包括加入密碼保護或加密。
engine.Compact("Data Source=; Password = a@3!7f$dQ;");
範例
此範例壓縮現有的 SQL Server Compact Edition 資料庫,並顯示如何變更資料庫屬性。
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;");
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;")
請參閱
概念
維護資料庫 (SQL Server Compact Edition)
一般資料庫工作 (SQL Server Compact Edition)