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)

Syntax

'Declaration
Public Property Connection As SqlCeConnection
public SqlCeConnection Connection { get; set; }
public:
property SqlCeConnection^ Connection {
    SqlCeConnection^ get ();
    void set (SqlCeConnection^ value);
}
/** @property */
public SqlCeConnection get_Connection ()

/** @property */
public void set_Connection (SqlCeConnection value)
public function get Connection () : SqlCeConnection

public function set Connection (value : SqlCeConnection)

Property Value

The connection to a data source. The default value is null.

Exceptions

Exception type Condition
InvalidOperationException

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

Remarks

SQL Server Compact 3.5 SP1 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 null, an InvalidOperationException is generated. If the Transaction property is not null and the transaction has already been committed or rolled back, Transaction is set to null.

Example

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;

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.

Platforms

Development Platforms

Windows Vista, Windows Mobile 5.0, Windows XP Professional with Service Pack 2 (SP2), Windows Server 2003, Windows Mobile 2003 for Pocket PC, Windows CE 5.0
Version Information
.NET Framework and NET Compact Framework
Supported in 3.5
.NET Framework
Supported in 3.0
.NET Compact Framework and .Net Framework
Supported in 2.0

See Also

Reference

SqlCeCommand Class
SqlCeCommand Members
System.Data.SqlServerCe Namespace
SqlCeCommand.CommandText Property
SqlCeCommand.CommandTimeout Property
CommandType