SqlCeConnection Class

Represents an open connection to a SQL Server Compact data source.

Inheritance Hierarchy


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


Public NotInheritable Class SqlCeConnection _
    Inherits DbConnection
Dim instance As SqlCeConnection
public sealed class SqlCeConnection : DbConnection
public ref class SqlCeConnection sealed : public DbConnection
type SqlCeConnection =  
        inherit DbConnection
public final class SqlCeConnection extends DbConnection

The SqlCeConnection type exposes the following members.


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



  Name Description
Protected property CanRaiseEvents (inherited from Component)
Public property ConnectionString Gets or sets the string used to open a database. (Overrides DbConnection.ConnectionString.)
Public property ConnectionTimeout Gets the time to wait while trying to establish a connection before terminating the attempt and generating an error. (Overrides DbConnection.ConnectionTimeout.)
Public property Container (inherited from Component)
Public property Database Gets the name of the current database or the database to be used when a connection is open. (Overrides DbConnection.Database.)
Public property DatabaseIdentifier Gets the unique identifier of the current database while synchronizing.
Public property DataSource Gets the file name of the data source. (Overrides DbConnection.DataSource.)
Protected property DbProviderFactory (inherited from DbConnection)
Protected property DesignMode (inherited from Component)
Protected property Events (inherited from Component)
Public property ServerVersion Returns the database version number as a string. (Overrides DbConnection.ServerVersion.)
Public property Site (inherited from Component)
Public property State Gets the current state of the connection. (Overrides DbConnection.State.)



  Name Description
Protected method BeginDbTransaction (inherited from DbConnection)
Public method BeginTransaction() Begins a database transaction.
Public method BeginTransaction(IsolationLevel) Begins a database transaction with the current IsolationLevel value.
Public method ChangeDatabase Changes the current database for an open SqlCeConnection. (Overrides DbConnection.ChangeDatabase(String).)
Public method Close Closes the connection to the data source. This is the preferred method of closing any open connection. (Overrides DbConnection.Close().)
Public method CreateCommand Creates and returns a SqlCeCommand object associated with the SqlCeConnection.
Protected method CreateDbCommand (inherited from DbConnection)
Public method CreateObjRef (inherited from MarshalByRefObject)
Public method Dispose() Releases all resources used by the current instance of the SqlCeConnection class.
Protected method Dispose(Boolean) (inherited from Component)
Public method EnlistTransaction(Transaction) (inherited from DbConnection)
Public method EnlistTransaction(Transaction) Enlists in the specified Transaction.
Public method Equals (inherited from Object)
Protected method Finalize (inherited from Component)
Public method GetDatabaseInfo Returns a set of Key Value pairs with information about locale, encryption mode, and case-sensitivity setting of the connected database.
Public method GetHashCode (inherited from Object)
Public method GetLifetimeService (inherited from MarshalByRefObject)
Public method GetSchema() Returns schema information for the data source of this SqlCeConnection. (Overrides DbConnection.GetSchema().)
Public method GetSchema(String) Returns schema information for the data source of this SqlCeConnection using the specified string for the schema name. (Overrides DbConnection.GetSchema(String).)
Public method GetSchema(String, array<String[]) Returns schema information for the data source of this SqlCeConnection using the specified string for the schema name and the specified string array for the restriction values. (Overrides DbConnection.GetSchema(String, array<String[]).)
Protected method GetService (inherited from Component)
Public method GetType (inherited from Object)
Public method InitializeLifetimeService (inherited from MarshalByRefObject)
Protected method MemberwiseClone() (inherited from Object)
Protected method MemberwiseClone(Boolean) (inherited from MarshalByRefObject)
Protected method OnStateChange (inherited from DbConnection)
Public method Open Opens a database connection with the property settings specified by the ConnectionString. (Overrides DbConnection.Open().)
Public method ToString (inherited from Component)



  Name Description
Public event Disposed (inherited from Component)
Public event FlushFailure Occurs when the background flush fails.
Public event InfoMessage Occurs when the .NET Compact Framework Data Provider for SQL Server sends a warning or an informational message.
Public event StateChange Occurs when the state of the connection changes. (Overrides DbConnection.StateChange.)


Explicit Interface Implementations

  Name Description
Explicit interface implemetationPrivate method IDbConnection.BeginTransaction() (inherited from DbConnection)
Explicit interface implemetationPrivate method IDbConnection.BeginTransaction(IsolationLevel) (inherited from DbConnection)
Explicit interface implemetationPrivate method IDbConnection.CreateCommand (inherited from DbConnection)



A SqlCeConnection object represents a unique connection to a data source. When you create an instance of SqlCeConnection, all properties are set to their initial values. For a list of these values, see the SqlCeConnection constructor.

If the SqlCeConnection goes out of scope, it is not closed. You must explicitly close the connection by calling Close or Dispose.

SQL Server Compact supports multiple simultaneous connections as well as multiple commands that share the same connection. This means that you can have multiple instances of SqlCeDataReader open on the same connection. This behavior differs from that of System.Data.SqlClient.

If a fatal SqlCeException is generated by the method executing a SqlCeCommand, the SqlCeConnection may be closed. You can reopen the connection and continue.


The following example creates a SqlCeCommand and a SqlCeConnection. The SqlCeConnection is opened and set as the Connection for the SqlCeCommand. The example then calls ExecuteNonQuery and closes the connection.

Dim conn As SqlCeConnection = Nothing

    conn = New SqlCeConnection("Data Source = MyDatabase.sdf; Password ='<pwd>'")

    Dim cmd As SqlCeCommand = conn.CreateCommand()
    cmd.CommandText = "INSERT INTO Customers ([Customer ID], [Company Name]) Values('NWIND', 'Northwind Traders')"

End Try
SqlCeConnection conn = null;

    conn = new SqlCeConnection("Data Source = MyDatabase.sdf; Password ='<pwd>'");

    SqlCeCommand cmd = conn.CreateCommand();
    cmd.CommandText = "INSERT INTO Customers ([Customer ID], [Company Name]) Values('NWIND', 'Northwind Traders')";


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


System.Data.SqlServerCe Namespace