SqlCeCommand.Transaction 属性

获取或设置执行 SqlCeCommand 的事务。

命名空间:  System.Data.SqlServerCe
程序集:  System.Data.SqlServerCe(在 System.Data.SqlServerCe.dll 中)

语法

声明
Public Property Transaction As SqlCeTransaction
    Get
    Set
用法
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);
}
member Transaction : SqlCeTransaction with get, set
function get Transaction () : SqlCeTransaction
function set Transaction (value : SqlCeTransaction)

属性值

类型:System.Data.SqlServerCe.SqlCeTransaction
SqlCeTransaction 。默认值为 nullnull 引用(在 Visual Basic 中为 Nothing)。

注释

如果 Transaction 属性已设置为特定值,而命令正在执行过程中,则不能设置该属性。如果设置了一个 SqlCeTransaction 对象的事务属性,而该对象没有与 SqlCeCommand 对象连接到相同的 SqlCeConnection,则在下次执行语句时将引发异常。

如果在将 SqlCeTransaction 设置为 nullnull 引用(在 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 ('Decker', 'Barbara')"
    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 ('Decker', 'Barbara')";
    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();
}

请参阅

参考

SqlCeCommand 类

System.Data.SqlServerCe 命名空间