如何在vb.net中使用Transact-SQL 事务,sqltransaction类的使用
如何在vb.net中使用Transact-SQL 事务,sqltransaction类的使用
表示要在 SQL Server 数据库中处理的 Transact-SQL 事务。不能继承此类。
有关此类型所有成员的列表,请参阅 SqlTransaction 成员。
System.Object
System.MarshalByRefObject
System.Data.SqlClient.SqlTransaction
[Visual Basic]NotInheritable Public Class SqlTransaction Inherits MarshalByRefObject Implements IDbTransaction, IDisposable
[C#]public sealed class SqlTransaction : MarshalByRefObject, IDbTransaction, IDisposable
[C++]public __gc __sealed class SqlTransaction : public MarshalByRefObject, IDbTransaction, IDisposable
[JScript]public class SqlTransaction extends MarshalByRefObject implements IDbTransaction, IDisposable
Shared)成员对多线程操作而言都是安全的。但不保证任何实例成员是线程安全的。
SqlTransaction 对象。对 SqlTransaction 对象执行与该事务关联的所有后面的操作(例如提交或中止该事务)。
[Visual Basic, C#] 下面的示例创建一个 SqlConnection 和一个 SqlTransaction。它还演示如何使用 BeginTransaction、Commit 和 Rollback 方法。
[Visual Basic] Public Sub RunSqlTransaction(myConnString As String) Dim myConnection As New SqlConnection(myConnString) myConnection.Open() Dim myCommand As SqlCommand = myConnection.CreateCommand() Dim myTrans As SqlTransaction ' Start a local transaction myTrans = myConnection.BeginTransaction() ' Must assign both transaction object and connection ' to Command object for a pending local transaction myCommand.Connection = myConnection myCommand.Transaction = myTrans Try myCommand.CommandText = 'Insert into Region (RegionID, RegionDescription) VALUES (100, 'Description')' myCommand.ExecuteNonQuery() myCommand.CommandText = 'Insert into Region (RegionID, RegionDescription) VALUES (101, 'Description')' myCommand.ExecuteNonQuery() myTrans.Commit() Console.WriteLine('Both records are written to database.') Catch e As Exception Try myTrans.Rollback() Catch ex As SqlException If Not myTrans.Connection Is Nothing Then Console.WriteLine('An exception of type ' & ex.GetType().ToString() & _ ' was encountered while attempting to roll back the transaction.') End If End Try Console.WriteLine('An exception of type ' & e.GetType().ToString() & _ 'was encountered while inserting the data.') Console.WriteLine('Neither record was written to database.') Finally myConnection.Close() End TryEnd Sub 'RunSqlTransaction
[C#] public void RunSqlTransaction(string myConnString) { SqlConnection myConnection = new SqlConnection(myConnString); myConnection.Open(); SqlCommand myCommand = myConnection.CreateCommand(); SqlTransaction myTrans; // Start a local transaction myTrans = myConnection.BeginTransaction(); // Must assign both transaction object and connection // to Command object for a pending local transaction myCommand.Connection = myConnection; myCommand.Transaction = myTrans; try { myCommand.CommandText = 'Insert into Region (RegionID, RegionDescription) VALUES (100, 'Description')'; myCommand.ExecuteNonQuery(); myCommand.CommandText = 'Insert into Region (RegionID, RegionDescription) VALUES (101, 'Description')'; myCommand.ExecuteNonQuery(); myTrans.Commit(); Console.WriteLine('Both records are written to database.'); } catch(Exception e) { try { myTrans.Rollback(); } catch (SqlException ex) { if (myTrans.Connection != null) { Console.WriteLine('An exception of type ' + ex.GetType() + ' was encountered while attempting to roll back the transaction.'); } } Console.WriteLine('An exception of type ' + e.GetType() + ' was encountered while inserting the data.'); Console.WriteLine('Neither record was written to database.'); } finally { myConnection.Close(); }}
[C++, JScript] 没有可用于 C++ 或 JScript 的示例。若要查看 Visual Basic 或 C# 示例,请单击页左上角的“语言筛选器”按钮
。