如何压缩数据库(以编程方式)

本主题将介绍如何使用 SqlServerCe.Engine 对象的 Compact 方法和使用 SqlServerCe.Engine 对象的 Shrink 方法压缩 SQL Server Compact 4.0 数据库。Compact 和 Shrink 方法在如何减小数据库大小方面稍有不同。

您可以使用 Compact 方法来回收数据库文件中的空间。也可以用它来更改数据库设置,如密码和区域设置 ID (LCID) 等设置。在压缩数据库时,将创建一个新的数据库文件,重新组织表页以使其驻留在相邻的数据库页中,并通过将所有数据库数据都重写到新的数据页中来回收未使用的空间。

您还可以使用 Shrink 方法来回收数据库文件中的空间。但是 Shrink 方法不能用来更改数据库设置,因为 Shrink 方法不创建新的数据库文件;它只是重新组织记录并删除空记录。

此外,本主题提供有关如何使用 Compact 方法更改 SQL Server Compact 数据库的区分大小写设置的信息。

有关 Compact 和 Shrink 的详细信息,请参阅维护数据库 (SQL Server Compact)。有关使用 SqlServerCe 命名空间的详细信息,请参阅 SqlServerCe 命名空间参考文档。

SQL Server Compact 4.0 的过程

压缩数据库

  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 SP1 版本开始,引入了区分大小写。有关详细信息,请参阅使用排序规则 (SQL Server Compact)

示例

下面的示例压缩一个现有 SQL Server Compact 数据库,并说明如何更改数据库属性。

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 数据库。

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

下面的示例演示如何使用 Compact 方法更改 SQL Server 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)