开始一个数据库事务。
语法
[Visual Basic]
Public Function BeginTransaction As GBaseTransaction
[C#]
public GBaseTransaction BeginTransaction()
返回值
代表新事务的对象。
注释
这个命令等价于在GBase 数据库中执行BEGIN TRANSACTION 命令,用户必
须使用 Commit 方法提交事务,使用Rollback 方法回滚事务。
如果用户没有指定一个隔离级别,会使用默认的隔离级别。要使用
GBase 8a 程序员手册ADO.NET 篇
南大通用数据技术股份有限公司
- 171 -
BeginTransaction 指定一个隔离级别,
可以使用带有IsolationLevel 的重载函
数。
异常
InvalidOperationException 异常
异常类型
条 件
InvalidOperationException
不支持并发事务。
示例
下面的例子中演示了如何使用BeginTransaction 的Commit 和 Rollback
方法。
[Visual Basic]
Public Sub RunTransaction(gsConnString As String)
Dim gsConnection As New GBaseConnection(gsConnString)
gsConnection.Open()
Dim gsCommand As GBaseCommand = gsConnection.CreateCommand()
Dim gsTrans As GBaseTransaction
' Start a local transaction
gsTrans = gsConnection.BeginTransaction()
' Must assign both transaction object and connection
' to Command object for a pending local transaction
gsCommand.Connection = gsConnection
gsCommand.Transaction = gsTrans
Try
gsCommand.CommandText
=
"Insert
into
Test
(id,
desc)
VALUES"
_ &" (100, 'Description')"
gsCommand.ExecuteNonQuery()
gsCommand.CommandText
=
"Insert
into
Test
(id,
desc)
VALUES"
_ &" (101, 'Description')"
gsCommand.ExecuteNonQuery()
gsTrans.Commit()
Console.WriteLine("Both records are written to
database.")
Catch e As Exception
Try
GBase 8a 程序员手册ADO.NET 篇
- 172 -
南大通用数据技术股份有限公司
gsTrans.Rollback()
Catch ex As GBaseException
If Not gsTrans.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
gsConnection.Close()
End Try
End Sub
[C#]
public void RunTransaction(string gsConnString)
{
GBaseConnection gsConnection = new
GBaseConnection(gsConnString);
gsConnection.Open();
GBaseCommand gsCommand = gsConnection.CreateCommand();
GBaseTransaction gsTrans;
// Start a local transaction
gsTrans = gsConnection.BeginTransaction();
// Must assign both transaction object and connection
// to Command object for a pending local transaction
gsCommand.Connection = gsConnection;
gsCommand.Transaction = gsTrans;
try
{
gsCommand.CommandText
=
"insert
into
Test
(id,
desc)
VALUES
(100, 'Description')";
gsCommand.ExecuteNonQuery();
gsCommand.CommandText
=
"insert
into
Test
(id,
desc)
VALUES
GBase 8a 程序员手册ADO.NET 篇
南大通用数据技术股份有限公司
- 173 -
(101, 'Description')";
gsCommand.ExecuteNonQuery();
gsTrans.Commit();
Console.WriteLine("Both
records
are
written
to
database.");
}
catch(Exception e)
{
try
{
gsTrans.Rollback();
}
catch (SqlException ex)
{
if (gsTrans.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
{
gsConnection.Close();
}
}