在 VB.NET 中:myOracleCommand.CommandText = _
"INSERT INTO product_types (" & _
" product_type_id, name" & _
") VALUES (" & _
" 3, 'Magazine'" & _
")"
第 5 步
使用 OracleCommand 对象的ExecuteNonQuery()方法运行INSERT语句。
在 C# 中:myOracleCommand.ExecuteNonQuery();
在 VB.NET 中:myOracleCommand.ExecuteNonQuery()
第 6 和第 7 步
将 OracleCommand 对象的 CommandText 属性设为向表 Products 中添加一行的第二条INSERT语句,并运行它。
在 C# 中:myOracleCommand.CommandText =
"INSERT INTO products (" +
" product_id, product_type_id, name, description, price" +
") VALUES (" +
" 5, 3, 'Oracle Magazine', 'Magazine about Oracle', 4.99" +
")";
myOracleCommand.ExecuteNonQuery();
在 VB.NET 中:myOracleCommand.CommandText = _
"INSERT INTO products (" & _
" product_id, product_type_id, name, description, price" & _
") VALUES (" & _
" 5, 3, 'Oracle Magazine', 'Magazine about Oracle', 4.99" & _
")"
myOracleCommand.ExecuteNonQuery()
第 8 步
使用 OracleTransaction 对象的Commit()方法提交数据库中的事务。
在 C# 中:myOracleTransaction.Commit();
在 VB.NET 中:myOracleTransaction.Commit()
在完成Commit()方法之后,由INSERT语句添加的两行将在数据库中永久记录。
第 9 步
使用Close()方法关闭 OracleConnection 对象。
在 C# 中:myOracleConnection.Close();
在 VB.NET 中:myOracleConnection.Close()
编译并运行示例程序
要编译 C# 示例程序,您可以使用 csc 命令运行 C# 编译器。因为程序使用 Oracle Data Access DLL,所以您应使用 /r 选项指定该 DLL 的完整路径,例如:
csc TransExample1.cs /r:C:\oracle\product\10.1.0\
Client_1\bin\Oracle.DataAccess.dll
注意:您需要用您计算机上的相应路径来替换该 DLL 的路径。此外,如果您的计算机找不到 csc 编译器,那么您可能需要运行 Microsoft sdkvars.bat 脚本来首先设置 .NET SDK 的环境变量;您可以在安装 .NET SDK 的 bin 目录中找到该脚本。
如果您遇到以下错误:
Example1.cs(10,7):error CS0246:The type or namespace name 'Oracle'
could not be found (are you missing a using directive or an assembly reference?)
这说明您没有在编译命令中正确指定 Oracle Data Access DLL。(有关设置的信息,请参阅 John Paul Cook 的技术文章“在 Oracle 数据库上构建 .NET 应用程序”。)
下面是用于编译 VB.NET 程序的等价命令:
vbc TransExample1.vb /r:C:\oracle\product\10.1.0\
Client_1\bin\Oracle.DataAccess.dll /r:system.dll /r:system.data.dll
接下来,输入以下命令,运行示例:
TransExample1
您将看到程序的输出。不过,如果您遇到类似以下的异常:
An exception was thrown
Message = ORA-12514:TNS:listener does not currently know
of service requested in connect descriptor
这说明 OracleConnection 对象的连接字符串中的 Data Source 的设置不正确。您应当咨询您的 DBA 或查阅 Oracle Net 文档以获得更多详细信息。
如果您使用的是 VS .NET,那么您可以遵循以下指示来编译和运行 C# 程序 TransExample1.cs:创建一个新的 C# 控制台应用程序。File>New Project,然后选择 Visual C# Projects,Console Application。
将项目命名为 TransExample1。
用 TransExample1.cs 中的代码替换 VS .NET 生成的所有代码。
选择 Project>Add Reference 添加对 Oracle.DataAccess.dll 的引用,然后浏览至您安装 ODP.NET 的目录(在我的计算机上,它是 C:\oracle\product\10.1.0\Client_1\bin\Oracle.DataAccess.dll),然后双击 Oracle.DataAccess.dll。
选择 Debug>Start without Debugging 运行该程序。
要编译和运行 TransExample1.vb,您可以执行类似的一系列步骤,但第 1 步应选择一个 Visual Basic 控制台应用程序,并在第 3 步用 TransExample1.vb 中的代码替换生成的代码。
查看程序的运行结果
当您运行完 C# 或 VB .NET 程序时,您可以在 SQL*Plus 中使用以下 SELECT 语句查看事务的结果:SELECT p.product_id, p.product_type_id, pt. name, p.name, p.description, p.price
FROM products p, product_types pt
WHERE p.product_type_id = pt.product_type_id
AND p.product_id = 5;您将看到以下结果: PRODUCT_ID PRODUCT_TYPE_ID NAME NAME
---------- --------------- ---------- -----------------------
DESCRIPTION PRICE
-------------------------------------------------- ----------
5 3 Magazine Oracle Magazine
Magazine about Oracle 4.99接下来,您将了解如何设置事务保存点。
在 .NET 程序中设置事务保存点
正如本文前面所提到的那样,您可以设置一个保存点,以便将事务回滚至该特定的点,同时保持事务中的其他语句原封不动。您可以使用 OracleTransaction 类的 Save() 方法在事务中设置保存点。
如果您有一个非常长的事务并且希望能够仅回滚到某个特定的时间点,那么您可能要使用保存点。例如,您可能想对 10 个产品做一些更改,然后设置一个保存点,然后再对另 10 个产品做更改;如果您在进行第二批更改时出现了错误,那么您可以回滚至保存点,使您的第一批更改原封不动。
我将带您逐步完成演示如何使用保存点的 C# (TransExample2.cs) 示例程序和 VB.NET (TransExample2.vb) 示例程序中的相关新步骤。这些程序向表 products 中添加一行,设置一个保存点,向表 products 中添加另一行,回滚至保存点,然后从表 products 中读取这些行。在回滚至保存点后,只有添加到表 products 中的第一行保留了下来:第二行将已被删除。
第 1 到第 3 步与“在 C# 和 VB.NET 中使用数据库事务”部分中所示的步骤相同,因此在这里将其省略。
第 4 步
向表 products 中添加一行,该行的产品 ID 为 6。
在 C# 中:myOracleCommand.CommandText =
"INSERT INTO products (" +
" product_id, product_type_id, name, description, price" +
") VALUES (" +
" 6, 2, 'Man from Another World', 'Man from Venus lands on Earth', 24.99" +
")";
myOracleCommand.ExecuteNonQuery();
在 VB.NET 中:myOracleCommand.CommandText = _
"INSERT INTO products (" & _
" product_id, product_type_id, name, description, price" & _
") VALUES (" & _
" 6, 2, 'Man from Another World', 'Man from Venus lands on Earth', 24.99" & _
")"
myOracleCommand.ExecuteNonQuery()
第 5 步
使用 OracleTransaction 的 Save() 方法设置一个名为 SaveProduct 的保存点。
在 C# 中:myOracleTransaction.Save("SaveProduct");
在 VB.NET 中:myOracleTransaction.Save("SaveProduct")
第 6 步
向表 products 中添加另一行,该行的产品 ID 为 7。
在 C# 中:myOracleCommand.CommandText =
"INSERT INTO products (" +
" product_id, product_type_id, name, description, price" +
") VALUES (" +
" 7, 2, 'Z-Files', 'Mysterious stories', 14.99" +
")";
myOracleCommand.ExecuteNonQuery();
在 VB.NET 中:myOracleCommand.CommandText = _
"INSERT INTO products (" & _
" product_id, product_type_id, name, description, price" & _
") VALUES (" & _
" 7, 2, 'Z-Files', 'Mysterious stories', 14.99" & _
")"
myOracleCommand.ExecuteNonQuery()
第 7 步
回滚到先前在第 5 步中设置的 SaveProduct 保存点。
在 C# 中:myOracleTransaction.Rollback("SaveProduct");
在 VB.NET 中:myOracleTransaction.Rollback("SaveProduct")
完成回滚后,在第 6 步中添加的第二行已被删除,而在第 4 步中添加的第一行保留了下来。
TransExample2.cs 和 TransExample2.vb 中剩下的步骤显示表 products 的内容,回滚整个事务并从数据库断开。
用于 Microsoft Transaction Server 的 Oracle 事务服务的快速说明
Microsoft Transaction Server 是一个运行在互联网或网络服务器上的专有事务处理系统。Microsoft Transaction Server 为客户端计算机部署和管理应用程序和数据库事务请求。
Microsoft Transaction Server 是以服务器为中心的三层体系结构模型的一个组件。这种方法实现了将应用程序的表示、业务逻辑和数据元素清晰地分布到在一个网络中连接的不同计算机上。无需专门集成,您就可以在与 Oracle 数据库服务器 8.0.6 版或更高版本连接的 Microsoft Transaction Server 中部署一个组件,但首先您必须安装 Oracle Services for Microsoft Transaction Server。
结论
在本文中,您系统学习了在 .NET 程序中使用数据库事务。您了解了如何创建 OracleTransaction 对象并用它们将事务提交给数据库,如何使用保存点部分回滚一个事务,以及 Oracle 数据库如何分离并发事务。