Aracılığıyla paylaş


SqlCeEngine Class

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

Inheritance Hierarchy

System.Object
  System.Data.SqlServerCe.SqlCeEngine

Namespace:  System.Data.SqlServerCe
Assembly:  System.Data.SqlServerCe (in System.Data.SqlServerCe.dll)

Syntax

'Declaration
Public NotInheritable Class SqlCeEngine _
    Implements IDisposable
'Usage
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

The SqlCeEngine type exposes the following members.

Constructors

  Name Description
Public method SqlCeEngine() Initializes a new instance of the SqlCeEngine class.
Public method SqlCeEngine(String) Initializes a new instance of the SqlCeEngine class with the specified local connection string.

Top

Properties

  Name Description
Public property LocalConnectionString Gets or sets the connection string to the SQL Server Compact database.

Top

Methods

  Name Description
Public method Compact Reclaims wasted space in the SQL Server Compact database by creating a new database file from the existing file. This method is also used to change the collating order, encryption, or password settings of the database.
Public method CreateDatabase Creates a new database.
Public method Dispose Releases all SQL Server Compact SqlCeEngine resources.
Public method Equals (inherited from Object)
Protected method Finalize (inherited from Object)
Public method GetHashCode (inherited from Object)
Public method GetType (inherited from Object)
Protected method MemberwiseClone (inherited from Object)
Public method Repair Repairs a corrupted database.
Public method Shrink Reclaims wasted space in the SQL Server Compact database by moving empty pages to the end of the file, and then truncating the file.
Public method ToString (inherited from Object)
Public method Upgrade() Upgrades a SQL Server Compact database from version 3.5 to 4.0. After the upgrade, the database will be encrypted if the source database was encrypted. If it was not, the upgraded database will be unencrypted.
Public method Upgrade(String) Upgrades a SQL Server Compact database from version 3.5 to 4.0. The destination database will be encrypted if the encryption mode was specified in the Destination Connection string. The collation of the database will be case sensitive if the case-sensitive property is set to true in the connection string.
Public method Verify() Recalculates the checksums for each page in the database and compares the new checksums to the expected values.
Public method Verify(VerifyOption) Verifies the integrity of the database based on the VerifyOption.

Top

Remarks

SQL Server Compact is optimized to serve as a database for Web sites and Web applications. SQL Server Compact is optimized for use as an embedded database within Web applications. SQL Server Compact can be used as a database for Web sites for multiple users and concurrent data changes.

SQL Server Compact can be used for developing ASP.NET Web applicatons. In the previous versions of SQL Server Compact a flag, SQLServerCompactEditionUnderWebHosting had to be set to use SQL Server Compact in ASP.NET Web applications. The flag is removed in SQL Server Compact 4.0.

Examples

The following sample demonstrates how to create a new SQL Server Compact 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.

See Also

Reference

System.Data.SqlServerCe Namespace