Engine オブジェクトを使用してデータベースを作成する方法 (プログラム)

ここでは、プログラムで SqlServerCe.Engine オブジェクトの CreateDatabase メソッドを使用して、SQL Server Compact データベースを作成する方法について説明します。SqlServerCe 名前空間の使用については、SqlServerCe 名前空間のリファレンス ドキュメントを参照してください。

SQL Server Compact 4.0 での手順

Engine オブジェクトを使用してデータベースを作成するには

  1. 新しい Engine オブジェクトを初期化します。

    SqlCeEngine engine = new SqlCeEngine();
    
  2. Engine オブジェクトの LocalConnectionString プロパティを設定します。LocalConnectionString プロパティで、作成するデータベースの名前と場所を指定します。また、データベースの追加オプション (暗号化など) を指定できます。

    eng.LocalConnectionString= "Data Source='Test.sdf'; LCID=1033;" +
       "Password='<enterStrongPasswordHere>';";
    
  3. CreateDatabase メソッドを呼び出し、データベースを作成します。

    engine.CreateDatabase();
    

Engine オブジェクトを使用して大文字と小文字が区別されるデータベースを作成するには

  1. 新しい Engine オブジェクトを初期化します。

    SqlCeEngine engine = new SqlCeEngine();
    
  2. Engine オブジェクトの LocalConnectionString プロパティを設定します。LocalConnectionString プロパティで、作成するデータベースの名前と場所を指定します。また、データベースの追加オプション (大文字と小文字の区別など) を指定できます。

    eng.LocalConnectionString= "Data Source='Test.sdf'; LCID=1033;" +
       "Password='<enterStrongPasswordHere>'; Case Sensitive = TRUE;";
    
  3. CreateDatabase メソッドを呼び出し、データベースを作成します。

    engine.CreateDatabase();
    

使用例

最初の例では、Test.sdf という名前の新しいデータベースを作成します。

System.IO.File.Delete("Test.sdf");
string connString = "Data Source = 'Test.sdf'; LCID=1033; Password = <enterStrongPasswordHere>;";
SqlCeEngine engine = new SqlCeEngine(connString);
engine.CreateDatabase();
System.IO.File.Delete("Test.sdf")
Dim connString As String = "Data Source='Test.sdf'; LCID=1033; Password=<enterStrongPasswordHere>; Encrypt = TRUE;"
Dim engine As New SqlCeEngine(connString)
engine.CreateDatabase()

2 番目の例では、LocalConnectionString プロパティを使用します。このプロパティは、"Case Sensitive" または "CaseSensitive" という追加の Boolean プロパティをサポートしています。このプロパティは、true または false の値をとります。既定値は常に false です。このプロパティは SQL Server Compact 3.5 SP1 リリースで導入されました。詳細については、「照合順序の使用 (SQL Server Compact)」を参照してください。

この例では、LocalConnectionString プロパティを使用して、接続文字列で大文字と小文字の区別のプロパティを設定する方法を示します。このコード例では、その後 GetDatabaseInfo メソッドを使用して、データベースのロケール、暗号化モード、および大文字と小文字の区別に関する設定を取得します。

// First approach: 
// Create a case-sensitive database by using the SqlCeEngine 
// constructor.
if (File.Exists("Test.sdf"))
    File.Delete("Test.sdf");

// 1033: specifies the English locale with collation 
// SQL_Latin1_General_CP1_CI_AS
string connStr = 
   "Data Source='Test.sdf'; LCID=1033; Case Sensitive = TRUE";

SqlCeEngine engine = new SqlCeEngine(connStr);
engine.CreateDatabase();
engine.Dispose();

// Second approach: 
// Create a case-sensitive database by using the LocalConnectionString 
// property.
if (File.Exists("Test.sdf"))
   File.Delete("Test.sdf");

SqlCeEngine engine2 = new SqlCeEngine();

engine2.LocalConnectionString = 
   "Data Source='Test.sdf'; LCID=1033; Case Sensitive = TRUE";
// 1033: specifies the English locale with collation 
// SQL_Latin1_General_CP1_CI_AS
engine2.CreateDatabase();
engine2.Dispose();

SqlCeConnection conn = null;

try
{
   conn = new SqlCeConnection(connStr);
   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);
   }
   }
   catch (Exception ex)
   {
      Console.WriteLine(ex.Message);
   }
   finally
   {
      conn.Close();
      Console.WriteLine("\n\n\nPress any key to continue...");
      Console.Read();
   }
' First approach: 
' Create a case sensitive database by using the SqlCeEngine 
' constructor.
If File.Exists("Test.sdf") Then
    File.Delete("Test.sdf")
End If

' 1033: specifies the English locale with collation 
' SQL_Latin1_General_CP1_CI_AS
Dim connStr As String = "Data Source='Test.sdf'; LCID=1033; Case Sensitive = TRUE"

Dim engine As New SqlCeEngine(connStr)
engine.CreateDatabase()
engine.Dispose()

' Second approach: 
' Create a case-sensitive database by using the LocalConnectionString 
' property.
If File.Exists("Test.sdf") Then
    File.Delete("Test.sdf")
End If

Dim engine2 As New SqlCeEngine
engine2.LocalConnectionString = _
    "Data Source='Test.sdf'; LCID=1033; Case Sensitive = TRUE"
    ' 1033: specifies the English locale with collation 
    ' SQL_Latin1_General_CP1_CI_AS
engine2.CreateDatabase()
engine2.Dispose()

Dim conn As SqlCeConnection = Nothing

Try
    conn = New SqlCeConnection(connStr)
    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(ChrW(10) & "GetDatabaseInfo() results:")

    Dim kvp As KeyValuePair(Of String, String)

    For Each kvp In dbinfo
        Console.WriteLine(kvp)
    Next
Catch ex As Exception
    Console.WriteLine(ex.Message)
Finally
    conn.Close()
    Console.WriteLine(vbNewLine & vbNewLine & vbNewLine & "Press any key to continue...")
    Console.Read()
End Try

関連項目

概念

データベース エンジンの概要 (SQL Server Compact)