SqlCeConnection Class

Represents an open connection to a data source.

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

Syntax

'Declaration
Public NotInheritable Class SqlCeConnection
    Inherits DbConnection
'Usage
Dim instance As SqlCeConnection
public sealed class SqlCeConnection : DbConnection
public ref class SqlCeConnection sealed : public DbConnection
public final class SqlCeConnection extends DbConnection
public final class SqlCeConnection extends DbConnection

Remarks

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 Mobile supports multiple simultaneous connections as well as multiple commands sharing the same connection. This means that it is possible to 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.

Example

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

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

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

    cmd.ExecuteNonQuery()
Finally
    conn.Close()
End Try
SqlCeConnection conn = null;

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

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

    cmd.ExecuteNonQuery();

finally
{
    conn.Close();

Inheritance Hierarchy

System.Object
   System.MarshalByRefObject
     System.ComponentModel.Component
       System.Data.Common.DbConnection
        System.Data.SqlServerCe.SqlCeConnection

Thread Safety

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

Platforms

Windows CE, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows XP Professional x64 Edition, Windows XP SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

Version Information

.NET Compact Framework

Supported in: 2.0, 1.0

See Also

Reference

SqlCeConnection Members
System.Data.SqlServerCe Namespace
SqlCeDataAdapter
SqlCeCommand Class