共用方式為


SqlCeCommand.Connection 屬性

取得或設定由 SqlCeCommand 的這個執行個體所使用的 SqlCeConnection

命名空間:  System.Data.SqlServerCe
組件:  System.Data.SqlServerCe (在 System.Data.SqlServerCe.dll 中)

語法

'宣告
Public Property Connection As SqlCeConnection
    Get
    Set
'用途
Dim instance As SqlCeCommand
Dim value As SqlCeConnection

value = instance.Connection

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

屬性值

型別:System.Data.SqlServerCe.SqlCeConnection
資料來源的連接。預設值為 nullnull 參考 (在 Visual Basic 中為 Nothing)。

例外狀況

例外狀況 條件
InvalidOperationException

當交易正在進行中時,Connection 屬性會變更。

備註

SQL Server Compact 可同時支援多個連接,並支援共用相同連接的多個命令。因此,在同一個連接上可以開啟 SqlCeDataReader 的多個執行個體。這項行為與 System.Data.SqlClient 的不同。

如果目前的連接正在執行「執行」或「擷取」作業,您就無法設定 Connection、CommandTypeCommandText 屬性。

如果在交易進行時設定 Connection,且 Transaction 屬性不是 nullnull 參考 (在 Visual Basic 中為 Nothing),則會產生 InvalidOperationException。如果 Transaction 屬性不是 nullnull 參考 (在 Visual Basic 中為 Nothing),且交易已經認可或回復,則 Transaction 會設定為 nullnull 參考 (在 Visual Basic 中為 Nothing)。

範例

下列範例會建立 SqlCeCommand,並將 Connection 屬性設定為 SqlCeConnection 物件。

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;

請參閱

參考

SqlCeCommand 類別

System.Data.SqlServerCe 命名空間

CommandText

CommandTimeout

CommandType