SqlCeCommand.Connection Property

Gets or sets the SqlCeConnection used by this instance of the SqlCeCommand.

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


Public Property Connection As SqlCeConnection
Dim instance As SqlCeCommand
Dim value As SqlCeConnection

value = instance.Connection

instance.Connection = value
public SqlCeConnection Connection { get; set; }
property SqlCeConnection^ Connection {
    SqlCeConnection^ get ();
    void set (SqlCeConnection^ value);
member Connection : SqlCeConnection with get, set
function get Connection () : SqlCeConnection
function set Connection (value : SqlCeConnection)

Property Value

Type: System.Data.SqlServerCe.SqlCeConnection
The connection to a data source. The default value is nulla null reference (Nothing in Visual Basic).


Exception Condition

The Connection property was changed while a transaction was in progress.


SQL Server Compact supports multiple simultaneous connections as well as multiple commands sharing the same connection. Thus, it is possible to have multiple instances of SqlCeDataReader open on the same connection. This behavior differs from that of System.Data.SqlClient.

You cannot set the Connection, CommandType, and CommandText properties if the current connection is performing an Execute or Fetch operation.

If you set Connection while a transaction is in progress and the Transaction property is not nulla null reference (Nothing in Visual Basic), an InvalidOperationException is generated. If the Transaction property is not nulla null reference (Nothing in Visual Basic) and the transaction has already been committed or rolled back, Transaction is set to nulla null reference (Nothing in Visual Basic).


The following example creates a SqlCeCommand and sets the Connection property to a SqlCeConnection object.

Dim queryText As String = "SELECT * FROM Categories ORDER BY [Category ID]"
Dim cmd As New SqlCeCommand(queryText)

cmd.Connection = conn
cmd.CommandType = CommandType.Text
string queryText = "SELECT * FROM Categories ORDER BY [Category ID]";
SqlCeCommand cmd = new SqlCeCommand(queryText);

cmd.Connection = conn;
cmd.CommandType = CommandType.Text;

See Also


SqlCeCommand Class

System.Data.SqlServerCe Namespace