.NET Framework Class Library  

SqlCeConnection Class

Note: This namespace, class, or member is supported only in version 1.1 of the .NET Framework.

Represents an open connection to a data source.

For a list of all members of this type, see SqlCeConnection Members.

System.Object
   System.MarshalByRefObject
      System.Data.SqlServerCe.SqlCeConnection

[Visual Basic]
NotInheritable Public Class SqlCeConnection
   Inherits MarshalByRefObject
   Implements IDbConnection, IDisposable
[C#]
public sealed class SqlCeConnection : MarshalByRefObject,
   IDbConnection, IDisposable
[C++]
public __gc __sealed class SqlCeConnection : public
   MarshalByRefObject, IDbConnection, IDisposable
[JScript]
public class SqlCeConnection extends MarshalByRefObject implements
   IDbConnection, IDisposable

Thread Safety

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

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.

Although SQL Server CE only supports one connection at a time, multiple commands can share 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

[Visual Basic, C#] 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.

[Visual Basic] 
Public Sub InsertRow(ByVal myConnectionString As String)
    ' If the connection string is null, use a default.
    If myConnectionString = "" Then
        myConnectionString = "Data Source = Northwind.sdf;"
    End If

    Dim myConnection  As New SqlCeConnection(myConnectionString)
    Dim myInsertQuery As String = "INSERT INTO Customers (CustomerID, CompanyName) Values('NWIND', 'Northwind Traders')"
    Dim myCommand     As New SqlCeCommand(myInsertQuery)
    myCommand.Connection = myConnection
    
    myConnection.Open()
    myCommand.ExecuteNonQuery()
    myCommand.Connection.Close()
End Sub 

[C#] 
public void InsertRow(string myConnectionString) {
    // If the connection string is null, use a default.
    if(myConnectionString == "") {
        myConnectionString = "Data Source = Northwind.sdf;";
    }
    SqlCeConnection myConnection = new SqlCeConnection(myConnectionString);
    
    string myInsertQuery   = "INSERT INTO Customers (CustomerID, CompanyName) Values('NWIND', 'Northwind Traders')";
    SqlCeCommand myCommand = new SqlCeCommand(myInsertQuery);
    myCommand.Connection   = myConnection;
    
    myConnection.Open();
    myCommand.ExecuteNonQuery();
    myCommand.Connection.Close();
}

[C++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Data.SqlServerCe

Platforms: .NET Compact Framework - Windows CE .NET

Assembly: System.Data.Sqlserverce (in System.Data.Sqlserverce.dll)

See Also

SqlCeConnection Members | System.Data.SqlServerCe Namespace | SqlCeDataAdapter | SqlCeCommand