SqlCeCommand 建構 (String, SqlCeConnection, SqlCeTransaction)
使用查詢的文字、SqlCeConnection 和 SqlCeTransaction 初始化 SqlCeCommand 類別的新執行個體。
命名空間: System.Data.SqlServerCe
組件: System.Data.SqlServerCe (在 System.Data.SqlServerCe.dll 中)
語法
'宣告
Public Sub New ( _
commandText As String, _
connection As SqlCeConnection, _
transaction As SqlCeTransaction _
)
'用途
Dim commandText As String
Dim connection As SqlCeConnection
Dim transaction As SqlCeTransaction
Dim instance As New SqlCeCommand(commandText, _
connection, transaction)
public SqlCeCommand(
string commandText,
SqlCeConnection connection,
SqlCeTransaction transaction
)
public:
SqlCeCommand(
String^ commandText,
SqlCeConnection^ connection,
SqlCeTransaction^ transaction
)
new :
commandText:string *
connection:SqlCeConnection *
transaction:SqlCeTransaction -> SqlCeCommand
public function SqlCeCommand(
commandText : String,
connection : SqlCeConnection,
transaction : SqlCeTransaction
)
參數
- commandText
型別:System. . :: . .String
查詢的文字。
- connection
型別:System.Data.SqlServerCe. . :: . .SqlCeConnection
SqlCeConnection ,代表與資料來源的連接。
- transaction
型別:System.Data.SqlServerCe. . :: . .SqlCeTransaction
SqlCeCommand 在其中執行的交易。
備註
下表顯示 SqlCeCommand 執行個體的初始屬性值。
屬性 |
初始值 |
---|---|
cmdText |
|
Text |
|
新的 SqlCeConnection,為 connection 參數的值。 |
您可以藉由設定相關的屬性來變更任何這些參數的值。
範例
下列範例會建立 SqlCeCommand,並設定其部份屬性。
Dim cmdText As String = "INSERT INTO FactSalesQuota " & _
"(EmployeeKey, TimeKey, SalesAmountQuota) " & _
"VALUES (2, 1158, 150000.00)"
Dim conn As New SqlCeConnection("Data Source = AdventureWorks.sdf;")
conn.Open()
' Start a local transaction; SQL Mobile supports the following
' isolation levels: ReadCommitted, RepeatableRead, Serializable
'
Dim tx As SqlCeTransaction = conn.BeginTransaction(IsolationLevel.ReadCommitted)
' By default, commands run in auto-commit mode;
'
Dim cmd As New SqlCeCommand(cmdText, conn, tx)
Try
cmd.ExecuteNonQuery()
' Commit the changes to disk if everything above succeeded;
' Use Deferred mode for optimal performance; the changes will
' be flashed to disk within the timespan specified in the
' ConnectionString 'FLUSH INTERVAL' property;
'
tx.Commit(CommitMode.Deferred)
' Alternatively, you could use:
' tx.Commit(CommitMode.Immediate);
'
' or use default (Deferred) commit mode:
' tx.Commit()
Catch e As Exception
' Handle errors here
'
tx.Rollback()
Finally
conn.Close()
End Try
string cmdText = "INSERT INTO FactSalesQuota " +
"(EmployeeKey, TimeKey, SalesAmountQuota) " +
"VALUES (2, 1158, 150000.00)";
SqlCeConnection conn = new SqlCeConnection("Data Source = AdventureWorks.sdf;");
conn.Open();
// Start a local transaction; SQL Mobile supports the following
// isolation levels: ReadCommitted, RepeatableRead, Serializable
//
SqlCeTransaction tx = conn.BeginTransaction(IsolationLevel.ReadCommitted);
SqlCeCommand cmd = new SqlCeCommand(cmdText, conn, tx);
try
{
cmd.ExecuteNonQuery();
// Commit the changes to disk if everything above succeeded;
// Use Deferred mode for optimal performance; the changes will
// be flushed to disk within the timespan specified in the
// ConnectionString 'FLUSH INTERVAL' property;
//
tx.Commit(CommitMode.Deferred);
// Alternatively, you could use:
// tx.Commit(CommitMode.Immediate);
//
// or use default (Deferred) commit mode:
// tx.Commit()
}
catch (Exception)
{
// Handle errors here
//
tx.Rollback();
}
finally
{
conn.Close();
}