SqlCeCommand.Transaction 属性
获取或设置执行 SqlCeCommand 的事务。
命名空间: System.Data.SqlServerCe
程序集: System.Data.SqlServerCe(在 system.data.sqlserverce.dll 中)
语法
声明
Public Property Transaction As SqlCeTransaction
用法
Dim instance As SqlCeCommand
Dim value As SqlCeTransaction
value = instance.Transaction
instance.Transaction = value
public SqlCeTransaction Transaction { get; set; }
public:
property SqlCeTransaction^ Transaction {
SqlCeTransaction^ get ();
void set (SqlCeTransaction^ value);
}
/** @property */
public SqlCeTransaction get_Transaction ()
/** @property */
public void set_Transaction (SqlCeTransaction value)
public function get Transaction () : SqlCeTransaction
public function set Transaction (value : SqlCeTransaction)
不适用。
属性值
SqlCeTransaction。默认值为 空引用(在 Visual Basic 中为 Nothing)。
备注
如果 Transaction 属性已设置为特定值,而命令正在执行过程中,则不能设置该属性。如果设置了一个 SqlCeTransaction 对象的事务属性,而该对象没有与 SqlCeCommand 对象连接到相同的 SqlCeConnection,则在下次执行语句时将引发异常。
如果在将 SqlCeTransaction 设置为 空引用(在 Visual Basic 中为 Nothing) 的情况下运行 SqlCeCommand,则该命令以自动提交模式运行;因此每个语句都使用其自身的事务运行。
示例
Dim conn As New SqlCeConnection("Data Source = MyDatabase.sdf; Password ='<pwd>'")
conn.Open()
' Start a local transaction
'
Dim tx As SqlCeTransaction = conn.BeginTransaction()
' By default, commands run in auto-commit mode;
'
Dim cmd1 As SqlCeCommand = conn.CreateCommand()
' You may create multiple commands on the same connection
'
Dim cmd2 As SqlCeCommand = conn.CreateCommand()
' To enlist a command in a transaction, set the Transaction property
'
cmd1.Transaction = tx
Try
cmd1.CommandText = "INSERT INTO Shippers ([Company Name]) VALUES ('Northwind Traders')"
cmd1.ExecuteNonQuery()
' Auto-commited because cmd2 is not enlisted in a transaction
'
cmd2.CommandText = "INSERT INTO Employees ([Last Name], [First Name]) VALUES ('Nancy', 'Smith')"
cmd2.ExecuteNonQuery()
' This will cause referential constraint violation
'
cmd1.CommandText = "DELETE FROM Products WHERE [Product ID] = 1"
cmd1.ExecuteNonQuery()
' Commit the changes to disk if everything above succeeded
'
tx.Commit()
Catch
tx.Rollback()
Finally
conn.Close()
End Try
SqlCeConnection conn = new SqlCeConnection("Data Source = MyDatabase.sdf; Password ='<pwd>'");
conn.Open();
// Start a local transaction
//
SqlCeTransaction tx = conn.BeginTransaction();
// By default, commands run in auto-commit mode;
//
SqlCeCommand cmd1 = conn.CreateCommand();
// You may create multiple commands on the same connection
//
SqlCeCommand cmd2 = conn.CreateCommand();
// To enlist a command in a transaction, set the Transaction property
//
cmd1.Transaction = tx;
try
{
cmd1.CommandText = "INSERT INTO Shippers ([Company Name]) VALUES ('Northwind Traders')";
cmd1.ExecuteNonQuery();
// Auto-commited because cmd2 is not enlisted in a transaction
//
cmd2.CommandText = "INSERT INTO Employees ([Last Name], [First Name]) VALUES ('Nancy', 'Smith')";
cmd2.ExecuteNonQuery();
// This will cause referential constraint violation
//
cmd1.CommandText = "DELETE FROM Products WHERE [Product ID] = 1";
cmd1.ExecuteNonQuery();
// Commit the changes to disk if everything above succeeded
//
tx.Commit();
}
catch (Exception)
{
tx.Rollback();
}
finally
{
conn.Close();
}
平台
Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
Windows Vista、Microsoft Windows XP SP2 和 Windows Server 2003 SP1 支持 Microsoft .NET Framework 3.0。
版本信息
.NET Framework
受以下版本支持:3.0
.NET Compact Framework
受以下版本支持:2.0、1.0