如何:使用 Engine 物件建立資料庫 (以程式設計方式)
在此主題中,您將了解如何使用 SqlServerCe.Engine 物件的 CreateDatabase 方法,以程式設計的方式建立 SQL Server Compact 資料庫。如需有關使用 SqlServerCe 命名空間的詳細資訊,請參閱 SqlServerCe 命名空間參考文件集。
SQL Server Compact 4.0 的程序
若要使用引擎物件建立資料庫
初始化新的 Engine 物件。
SqlCeEngine engine = new SqlCeEngine();
設定 Engine 物件的 LocalConnectionString 屬性。此 LocalConnectionString 屬性會指定將要建立的資料庫名稱與位置,而且可能指定其他額外的資料庫選項,包括加密。
eng.LocalConnectionString= "Data Source='Test.sdf'; LCID=1033;" + "Password='<enterStrongPasswordHere>';";
呼叫 CreateDatabase 方法建立資料庫。
engine.CreateDatabase();
使用 Engine 物件建立區分大小寫的資料庫
初始化新的 Engine 物件。
SqlCeEngine engine = new SqlCeEngine();
設定 Engine 物件的 LocalConnectionString 屬性。此 LocalConnectionString 屬性會指定將要建立之資料庫的名稱與位置,而且可能指定其他額外的資料庫選項,包括區分大小寫。
eng.LocalConnectionString= "Data Source='Test.sdf'; LCID=1033;" + "Password='<enterStrongPasswordHere>'; Case Sensitive = TRUE;";
呼叫 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()
第二個範例會使用 LocalConnectionString 屬性,此屬性支援其他稱為 "Case Sensitive" 或 "CaseSensitive" 的 Boolean 屬性。此屬性的值可以是 true 或 false。它的預設值一定是 false。從 SQL Server Compact 3.5 SP1 版本開始導入了這個屬性。如需詳細資訊,請參閱<使用定序 (SQL Server Compact)>。
此範例示範如何在連接字串中,利用 LocalConnectionString 屬性來設定 case-sensitive 屬性。然後,此程式碼範例會使用 GetDatabaseInfo 方法來擷取資料庫的地區設定、加密模式和 case-sensitivity 設定:
// 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