共用方式為


如何:壓縮資料庫 (以程式設計的方式)

在此主題中,您將了解如何使用 SqlServerCe.Engine 物件的 Compact 方法和 SqlServerCe.Engine 物件的 Shrink 方法來壓縮 SQL Server Compact 3.5 資料庫。CompactShrink 方法縮減資料庫大小的方法稍微不同。

您可以使用 Compact 方法來回收資料庫檔案中的空間。您也可以使用這種方法來變更資料庫設定,例如密碼和地區設定識別碼 (LCID) 等設定。當您以 Compact 方法壓縮資料庫時,系統會建立新的資料庫檔案和重組資料表頁面,以使這些頁面存在於相鄰的資料庫頁面中,並將所有的資料庫資料重新寫入新資料頁面中,以回收未使用的空間。

您也可以使用 Shrink 方法來回收資料庫檔案中的空間。不過,由於 Shrink 方法不會建立新的資料庫檔案,而只會重組記錄並刪除空白記錄,因此您無法使用 Shrink 方法來變更資料庫設定。

此外,本主題也會提供有關如何使用 Compact 方法來變更 資料庫之區分大小寫設定的資訊。

如需有關 CompactShrink 的詳細資訊,請參閱<維護資料庫 (SQL Server Compact)>。如需使用 SqlServerCe 命名空間的詳細資訊,請參閱 SqlServerCe 命名空間的參考說明文件。

SQL Server Compact 3.5 的程序

若要壓縮資料庫

  1. 建立 Engine 物件,並傳遞到您要壓縮之現有資料庫的連接字串中。

    SqlCeEngine engine = new SqlCeEngine("Data Source = AdWks.sdf");
    
  2. 呼叫 Compact 方法。當呼叫 Compact 方法時,也可以指定新的資料庫屬性,包括加入密碼保護或加密。

    engine.Compact("Data Source=; Password = <enterStrongPasswordHere>");
    

若要壓縮資料庫

  1. 建立 Engine 物件,並傳入您要壓縮之資料庫的連接字串。

    SqlCeEngine engine = new SqlCeEngine("Data Source = AdWks.sdf");
    
  2. 呼叫 Shrink 方法。

    engine.Shrink();
    

變更壓縮資料庫的區分大小寫設定

  1. 建立 Engine 物件,並傳遞到您要壓縮之現有資料庫的連接字串中。

    SqlCeEngine engine = 
         new SqlCeEngine("Data Source= Test.sdf; LCID= 1033");
    
  2. 呼叫 Compact 方法。當您呼叫 Compact 方法時,也可以指定新的資料庫屬性,如區分大小寫。如果您在呼叫 Compact 方法時並未指定 "Case Sensitive",就不會變更區分大小寫的設定。

    engine.Compact("Data Source= Test.sdf; LCID= 1033; Case Sensitive=true");
    

注意

SQL Server Compact 3.5 SP1 版本中開始導入區分大小寫的設定。如需詳細資訊,請參閱<使用定序 (SQL Server Compact)>。

範例

此範例壓縮現有的 SQL Server Compact 3.5 資料庫,並顯示如何變更資料庫屬性。

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
//
engine.Compact("Data Source=; Password =<enterStrongPasswordHere>");
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
'
engine.Compact("Data Source=; Password =<enterStrongPasswordHere>")

本範例壓縮現有的 SQL Server Compact 3.5 資料庫。

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

下列範例示範如何使用 Compact 方法來變更 資料庫的區分大小寫設定。然後,此程式碼範例會呼叫 GetDatabaseInfo 方法來擷取資料庫的地區設定、加密模式和區分大小寫的值。

// Default case-insentive connection string.
string connStringCI = "Data Source= Test.sdf; LCID= 1033";

// Set "Case Sensitive" to true to change the collation from CI to CS.
string connStringCS = 
    "Data Source= Test.sdf; LCID= 1033; Case Sensitive=true"; 

if (File.Exists("Test.sdf"))
{
   File.Delete("Test.sdf");
}

SqlCeEngine engine = new SqlCeEngine(connStringCI);
// The collation of the database is case-insensitive.
engine.CreateDatabase();

// The collation of the database will be case-sensitive because of 
// the new connection string used by the Compact method.  
engine.Compact(connStringCS);

SqlCeConnection conn = null;
conn = new SqlCeConnection(connStringCS);
conn.Open();

//Retrieve the connection string information - notice the 'Case 
// Sensitive' value.
List<KeyValuePair<string, string>> dbinfo = conn.GetDatabaseInfo();

Console.WriteLine("\nGetDatabaseInfo() results:");

foreach (KeyValuePair<string, string> kvp in dbinfo)
{
    Console.WriteLine(kvp);
}
' Default case-insentive connection string.
Dim connStringCI As String = "Data Source= Test.sdf; LCID= 1033"

' Set "Case Sensitive" to true to change the collation from CI to CS.
Dim connStringCS As String = "Data Source= Test.sdf; LCID= 1033; Case Sensitive=true"

If File.Exists("Test.sdf") Then
    File.Delete("Test.sdf")
End If

Dim engine As New SqlCeEngine(connStringCI)
' The collation of the database is case insensitive.
engine.CreateDatabase()

' The collation of the database will be case sensitive because of 
' the new connection string used by the Compact method.
engine.Compact(connStringCS)

Dim conn As SqlCeConnection = Nothing
conn = New SqlCeConnection(connStringCS)
conn.Open()

'Retrieve the connection string information - notice the 'Case Sensitive' value.
Dim dbinfo As List(Of KeyValuePair(Of String, String)) = conn.GetDatabaseInfo

Console.WriteLine(vbNewLine & "GetDatabaseInfo() results:")

Dim kvp As KeyValuePair(Of String, String)
For Each kvp In dbinfo
    Console.WriteLine(kvp)
Next

另請參閱

其他資源

維護資料庫 (SQL Server Compact)
一般資料庫工作 (SQL Server Compact)

說明及資訊

取得協助 (SQL Server Compact 3.5 Service Pack 1)