SqlCeEngine 类

表示 SQL Server Compact 3.5 引擎对象的属性、方法及其他对象。无法继承此类。

命名空间:  System.Data.SqlServerCe
程序集:  System.Data.SqlServerCe(在 System.Data.SqlServerCe.dll 中)

语法

声明
Public NotInheritable Class SqlCeEngine _
    Implements IDisposable
用法
Dim instance As SqlCeEngine
public sealed class SqlCeEngine : IDisposable
public ref class SqlCeEngine sealed : IDisposable
[<SealedAttribute>]
type SqlCeEngine =  
    class
        interface IDisposable
    end
public final class SqlCeEngine implements IDisposable

注释

SQL Server Compact 3.5 并未针对网站数据库用途进行优化。默认情况下,在 SQL Server Compact 3.5 中,来自与 ASP.NET 相连的应用程序的连接将受到阻止。SQL Server Compact 3.5 已经过优化,可用作应用程序内的嵌入式数据库。将 SQL Server Compact 3.5 用作网站的数据库需要多用户和并发数据更改的支持。这可能引起性能问题。因此,不支持这些方案。SQL Server 的其他版本(包括 SQL Server 2005 Express Edition 和更高版本)针对网站数据库用途进行了优化。

在使用 ASP.NET 为同步方案创建 SQL Server Compact 3.5 数据库的应用方案中,可将 SQL Server Compact 3.5 与 ASP.NET 一起使用。使用下面的代码更改 SQL Server Compact 3.5 的默认行为,以便在 ASP.NET 内使用。

AppDomain.CurrentDomain.SetData("SQLServerCompactEditionUnderWebHosting", true)

示例

下面的示例说明如何创建一个新的 SQL Server Compact 3.5 数据库。

If File.Exists("Test.sdf") Then
   File.Delete("Test.sdf")
End If 
Dim connStr As String = "Data Source = Test.sdf; Password = <password>"

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

Dim conn As SqlCeConnection = Nothing

Try
   conn = New SqlCeConnection(connStr)
   conn.Open()

   Dim cmd As SqlCeCommand = conn.CreateCommand()
   cmd.CommandText = "CREATE TABLE myTable (col1 int, col2 ntext)"
   cmd.ExecuteNonQuery()
Catch
Finally
   conn.Close()
End Try
if (File.Exists("Test.sdf"))
    File.Delete("Test.sdf");

string connStr = "Data Source = Test.sdf; Password = <password>";

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

SqlCeConnection conn = null;

try {
    conn = new SqlCeConnection(connStr);
    conn.Open();

    SqlCeCommand cmd = conn.CreateCommand();
    cmd.CommandText = "CREATE TABLE myTable (col1 int, col2 ntext)";
    cmd.ExecuteNonQuery();
}
catch {}
finally {
    conn.Close();
}

继承层次结构

System. . :: . .Object
  System.Data.SqlServerCe..::..SqlCeEngine

线程安全

Any public static (Shared in Microsoft Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

请参阅

参考

SqlCeEngine 成员

System.Data.SqlServerCe 命名空间