详谈如何在VB.NET使用ORACLR数据库事务

开发 后端
文章主要教你在VB.NET使用Oracle数据库事务,有详细的9大步骤,代码的分析和错误的原因以及运行的结果。总之看了以后一定会使用oracle。

大家都知道VB.NET,oracle,但是如何在VB.NET使用ORACLE数据库呢,我想这个问题很多人都不知道怎么解决,在这里给大家演示一个示例吧。Oracle.DataAccess.Client 命名空间是 ODP.NET 的一部分,它包含许多类,其中有OracleConnection、OracleCommand 和 OracleTransaction。示例程序用到了这些类。

VB.NET使用ORACLR第 1 步

创建一个 OracleConnection 对象连接到 Oracle 数据库,然后打开该连接。
在 C# 中:

  1. OracleConnection myOracleConnection =new OracleConnection(  
  2. "User Id=store;Password=store;Data Source=ORCL"  
  3. );  
  4. myOracleConnection.Open(); 

在 VB.NET 中:

  1. Dim myOracleConnection As New OracleConnection( _  
  2. "User Id=store;Password=store;Data Source=ORCL")  
  3. myOracleConnection.Open() 

User Id 和 Password 属性指定了您所要连接到的模式的数据库用户和口令。Data Source 属性指定了数据库的 Oracle Net 服务名称;初始数据库的默认服务名称为 ORCL。如果您使用的不是初始数据库,或者您的服务名称不同,那么您需要在程序中修改 Data Source 属性的设置。

VB.NET使用ORACLR第 2 步

创建一个 OracleTransaction 对象,然后调用 OracleConnection 对象的 BeginTransaction() 方法启动事务。

在 C# 中:

  1. OracleTransaction myOracleTransaction =  
  2. myOracleConnection.BeginTransaction();  
  3. In VB.NET:  
  4. Dim myOracleTransaction As OracleTransaction = _ 
  5. myOracleConnection.BeginTransaction() 

VB.NET使用ORACLR第3 步

创建一个 OracleCommand 对象,用于存储 SQL 语句。

在 C# 中:

  1. OracleCommand myOracleCommand = myOracleConnection.CreateCommand();    

在 VB.NET 中:

  1. Dim myOracleCommand As OracleCommand =  
  2. myOracleConnection.CreateCommand    

因为 OracleCommand 对象使用 OracleConnection 对象的 CreateCommand() 方法创建的,所以它自动使用在第 2 步中为 OracleConnection 对象设置的事务。

VB.NET使用ORACLR第 4 步

将 OracleCommand 对象的 CommandText 属性设为向表 product_types 中添加一行的第一条 INSERT 语句。
在 C# 中:

  1. myOracleCommand.CommandText =  
  2. "INSERT INTO product_types (" +  
  3. "  product_type_id, name" +  
  4. ") VALUES (" +  
  5. "  3, 'Magazine'" +  
  6. ")"; 

 在 VB.NET 中:

  1. myOracleCommand.CommandText = _ 
  2. "INSERT INTO product_types (" & _  
  3. "  product_type_id, name" & _  
  4. ") VALUES (" & _  
  5. "  3, 'Magazine'" & _  
  6. ")" 

VB.NET使用ORACLR第 5 步
使用 OracleCommand 对象的 ExecuteNonQuery() 方法运行 INSERT 语句。
在 C# 中:

  1. myOracleCommand.ExecuteNonQuery(); 

在 VB.NET 中:

  1. myOracleCommand.ExecuteNonQuery(); 

VB.NET使用ORACLR第 6 和第 7 步

将 OracleCommand 对象的 CommandText 属性设为向表 Products 中添加一行的第二条 INSERT 语句,并运行它。
在 C# 中:

  1. myOracleCommand.CommandText =  
  2. "INSERT INTO products (" +  
  3. "  product_id, product_type_id, name, description, price" +  
  4. ") VALUES (" +  
  5. "  5, 3, 'Oracle Magazine', 'Magazine about Oracle', 4.99" +  
  6. ")";  
  7. myOracleCommand.ExecuteNonQuery();  

在 VB.NET 中:

  1. myOracleCommand.CommandText = _ 
  2. "INSERT INTO products (" & _  
  3. "  product_id, product_type_id, name, description, price" & _  
  4. ") VALUES (" & _  
  5. "  5, 3, 'Oracle Magazine', 'Magazine about Oracle', 4.99" & _  
  6. ")"  
  7. myOracleCommand.ExecuteNonQuery() 

VB.NET使用ORACLR第 8 步

使用 OracleTransaction 对象的 Commit() 方法提交数据库中的事务。
在 C# 中:

  1. myOracleTransaction.Commit(); 

在 VB.NET 中:

  1. myOracleTransaction.Commit() 

在完成 Commit() 方法之后,由 INSERT 语句添加的两行将在数据库中永久记录。


VB.NET使用ORACLR第 9 步

使用 Close() 方法关闭 OracleConnection 对象。

 在 C# 中:

  1. myOracleConnection.Close(); 

在 VB.NET 中:

  1. myOracleConnection.Close() 


编译并运行示例程序

要编译 C# 示例程序,您可以使用 csc 命令运行 C# 编译器。因为程序使用 Oracle Data Access DLL,所以您应使用 /r 选项指定该 DLL 的完整路径,例如:

注意:您需要用您计算机上的相应路径来替换该 DLL 的路径。此外,如果您的计算机找不到 csc 编译器,那么您可能需要运行 Microsoft sdkvars.bat 脚本来首先设置 .NET SDK 的环境变量;您可以在安装 .NET SDK 的 bin 目录中找到该脚本。

如果您遇到以下错误:

  1. Example1.cs(10,7):error CS0246:The type or namespace name 'Oracle'  
  2. could not be found (are you missing a using   
  3. directive or an assembly reference?) 

这说明您没有在编译命令中正确指定 Oracle Data Access DLL。(有关设置的信息,请参阅 John Paul Cook 的技术文章“在 Oracle 数据库上构建 .NET 应用程序”。)

下面是用于编译 VB.NET 程序的等价命令:

  1. vbc TransExample1.vb /r:C:\oracle\product\10.1.0\  
  2. Client_1\bin\Oracle.DataAccess.dll /r:system.dll /r:system.data.dll    

 接下来,输入以下命令,运行示例:

  1. An exception was thrown  
  2. Message = ORA-12514:TNS:listener does not currently know  
  3. 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 语句查看事务的结果:

  1. SELECT p.product_id, p.product_type_id, pt. name, p.name, p.description, p.price  
  2. FROM products p, product_types pt  
  3. WHERE p.product_type_id = pt.product_type_id  
  4. AND p.product_id = 5;  
  5. 您将看到以下结果: PRODUCT_ID PRODUCT_TYPE_ID NAME       NAME  
  6. ---------- --------------- ---------- -----------------------  
  7. DESCRIPTION                                             PRICE  
  8. -------------------------------------------------- ----------  
  9. 5 3 Magazine   Oracle Magazine  
  10. Magazine about Oracle                                    4.99   

【编辑推荐】

  1. 介绍VB.NET绘图方法的三个方面
  2. 突破VB.NET命名空间两大难关
  3. 演示VB.NET类定义全过程
  4. VB.NET类对象语法概括
  5. 讲述VB.NET实现拖动图片
责任编辑:田树 来源: 中科软件园
相关推荐

2009-10-23 17:03:18

VB.NET事件编程

2009-10-26 13:13:17

VB.NET编码规范

2009-10-09 15:20:26

VB.NET连接数据库

2009-10-28 17:00:30

VB.NET数据库

2009-10-28 17:08:57

VB.NET数据库开发

2010-01-08 10:37:50

VB.NET数据库

2009-10-13 17:31:50

VB.NET Acce

2009-11-11 11:33:08

VB.NET线程访问数

2009-10-28 16:47:26

VB.NET访问数据库

2010-01-12 10:40:58

VB.NET数据库压缩

2010-01-15 19:24:42

2010-01-15 18:24:14

VB.NET打开Not

2009-10-28 17:24:19

VB.NET介绍

2010-01-18 19:21:51

VB.NET存取数据库

2010-01-13 15:01:13

VB.NET操作MyS

2009-01-19 09:14:31

.NETMySQLMySql驱动包

2010-01-12 09:51:07

VB.NET操作dbf

2009-10-29 17:33:51

VB.NET线程方法

2010-01-07 17:24:12

VB.NET连接数据库

2010-01-18 17:45:33

VB.NET线程访问数
点赞
收藏

51CTO技术栈公众号