방법: 데이터베이스 압축(프로그래밍 방식)

이 항목에서는 SqlServerCe.Engine 개체의 Compact 메서드 및 SqlServerCe.Engine 개체의 Shrink 메서드를 사용하여 SQL Server Compact 4.0 데이터베이스를 압축하는 방법에 대해 설명합니다. Compact 메서드와 Shrink 메서드는 데이터베이스 크기가 줄어드는 정도가 약간 다릅니다.

Compact 메서드를 사용하면 데이터베이스 파일의 공간을 회수할 수 있으며 암호 및 LCID(로캘 ID)를 비롯한 데이터베이스 설정을 변경할 수도 있습니다. 데이터베이스를 압축하면 새 데이터베이스 파일이 생성되고 테이블 페이지가 데이터베이스 페이지 주위로 재구성되며, 데이터베이스 데이터가 새 데이터 페이지에 다시 기록되어 사용하지 않은 공간을 회수할 수 있습니다.

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 메서드를 호출할 때 "대/소문자 구분"을 지정하지 않으면 대/소문자 구분 설정이 변경되지 않습니다.

    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)