SqlCeEngine Class

Represents the properties, methods, and other objects of the SQL Server Compact 3.5 SP1 Engine object. This class cannot be inherited.

Namespace: System.Data.SqlServerCe
Assembly: System.Data.SqlServerCe (in system.data.sqlserverce.dll)

Syntax

'Declaration
Public NotInheritable Class SqlCeEngine
    Implements IDisposable
public sealed class SqlCeEngine : IDisposable
public ref class SqlCeEngine sealed : IDisposable
public final class SqlCeEngine implements IDisposable
public final class SqlCeEngine implements IDisposable

Remarks

SQL Server Compact 3.5 SP1 is not optimized to serve as a database for Web sites. By default, connections from ASP.NET-connected applications are blocked in SQL Server Compact 3.5 SP1. SQL Server Compact 3.5 SP1 is optimized for use as an embedded database within applications. Using SQL Server Compact 3.5 SP1 as a database for Web sites requires support for multiple users and concurrent data changes. This can cause performance problems. Therefore, these scenarios are not supported. Other editions of SQL Server, including SQL Server 2005 Express Edition and later versions, are optimized to serve as a database for Web sites.

SQL Server Compact 3.5 SP1 can be used with ASP.NET in application scenarios where ASP.NET is used to create SQL Server Compact 3.5 SP1 databases for synchronization scenarios. Use the following code to change the default behavior of SQL Server Compact 3.5 SP1 to work within ASP.NET.

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

Inheritance Hierarchy

System.Object
  System.Data.SqlServerCe.SqlCeEngine

Example

The following sample demonstrates how to create a new SQL Server Compact 3.5 SP1 database.

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();
}

Thread Safety

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.

Platforms

Development Platforms

Windows Vista, Windows Mobile 5.0, Windows XP Professional with Service Pack 2 (SP2), Windows Server 2003, Windows Mobile 2003 for Pocket PC, Windows CE 5.0
Version Information
.NET Framework and NET Compact Framework
Supported in 3.5
.NET Framework
Supported in 3.0
.NET Compact Framework and .Net Framework
Supported in 2.0

See Also

Reference

SqlCeEngine Members
System.Data.SqlServerCe Namespace