Connection Property

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 SqlCeConnection Connection { get; set; }

Property Value

Type: System.Data.SqlServerCe.SqlCeConnection
The connection to a data source. The default value is null.


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 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.

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

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

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

© 2016 Microsoft