SqlCeConnection Class

Represents an open connection to a  data source.

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


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.

 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.

 is not currently optimized to serve as a database for Web sites. By default, connections from -connected applications are blocked in . is optimized for use as an embedded database within applications. Using 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 , including , are optimized to serve as a database for Web sites.

 can be used with in application scenarios where is used to create  databases for synchronization scenarios. Use the following code to change the default behavior of to work within .

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


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')";


Inheritance Hierarchy

System. . :: . .Object
  System. . :: . .MarshalByRefObject
    System.ComponentModel. . :: . .Component
      System.Data.Common. . :: . .DbConnection

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


SqlCeConnection Members

System.Data.SqlServerCe Namespace