Share via


SqlCeEngine.Compact 方法

從現有的檔案建立新的資料庫檔案,以回收在 SQL Server Compact 資料庫中浪費的空間。這個方法也用於變更資料庫的定序排序、加密或密碼設定。

命名空間:  System.Data.SqlServerCe
組件:  System.Data.SqlServerCe (在 System.Data.SqlServerCe.dll 中)

語法

'宣告
Public Sub Compact ( _
    connectionString As String _
)
'用途
Dim instance As SqlCeEngine
Dim connectionString As String

instance.Compact(connectionString)
public void Compact(
    string connectionString
)
public:
void Compact(
    String^ connectionString
)
member Compact : 
        connectionString:string -> unit 
public function Compact(
    connectionString : String
)

參數

  • connectionString
    型別:System.String
    目的地資料庫的連接字串。

備註

連接字串指定由這個方法建立之目的地資料庫的連接。

如果指定的資料庫已存在,或者其他具有相同名稱的檔案已存在,就會擲回例外狀況。.

如果對連接字串傳遞空字串,則新資料庫檔案會覆寫舊資料庫檔案,並維持相同名稱。

重要

Compact 作業完成前應用程式不正常終止,會在系統中留下暫存資料庫檔案。此檔案的名稱為 <filename>pack.sdf,必須手動移除。

範例

下列範例示範如何壓縮 SQL Server Compact 資料庫。

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;");

請參閱

參考

SqlCeEngine 類別

System.Data.SqlServerCe 命名空間