zoukankan      html  css  js  c++  java
  • 将数组绑定到 ODP.NET 数据库命令

    申明:此文章摘自OTN(http://www.oracle.com/technology/global/cn/sample_code/tech/windows/odpnet/howto/arraybind/index.html)
    目标

    在阅读此方法文档后,您应能够:

      使用 ODP.NET 调用数据库存储过程

      使用 ODP.NET 的数组绑定功能

    前提

    假设读者熟悉 Visual Studio.NET,还了解 ODP.NET 和数据库的基础知识。

    简介

    此文档展示如何使用 ODP.NET 的“数组绑定”功能,在一次数据库回程中多次执行某个数据库存储过程。“数组绑定”;功能(其用法由OracleCommandArrayBindCount属性指定)允许将数组中的每个值作为一个参数,在一次回程中全部传递。ArrayBindCount属性确定命令的执行次数及作为OracleCommand一部分绑定的数组元素的个数。

    本文档中,使用一个称为Test_Arraybind的数据库存储过程。该存储过程将数据插入到表中,并由控制台应用程序调用。pdeptnopdname是传递给此存储过程的两个参数。系统将DeptNoDname的多个行存储在绑定到OracleParameters的数组中,而后者又被添加到执行存储过程的OracleCommand对象中。一旦执行Test_Arraybind,,系统将多个行作为IN参数传递,演示了在一次回程中如何将多个行传递给某个数据库存储过程。注意:尽管此文档使用存储过程,数组绑定功能还可用于常规 SQL 命令和 PL/SQL 块。

    价值定位

    数组绑定功能用于批量操作,其中一个存储过程或 SQL 语句在一个服务器回程中执行 ArrayBindCount 所指定的次数。每次执行使用参数(数组)中的第 n 个元素并执行存储过程或 SQL 语句 — 这个过程在数据库内部完成,它与存储过程或 SQL 语句无关。

    与 PL/SQL 关联数组相比,数组绑定是使用 ODP.NET 从 .NET 中进行大量插入的最佳方法,尤其是因为 PL/SQL 关联数组有以下缺点:

    • 必须编写一个 PL/SQL 过程来实现插入 — 虽然这将把数据以一个块的形式传送给服务器上的 PL/SQL 引擎,但它只允许一次一行地将数据插入到 SQL 引擎中

    相反,由于以下优点,使用数组绑定功能要比使用 PL/SQL 关联数组简单得多:

      控制批量大小:内置有一个控制批量大小的按钮。提高速度:由于一并将行数据数组直接复制到 SQL 引擎中,因此速度更快。

    需要

      Oracle Data Provider for .NET (ODP.NET)

       

      运行 SQL*Net TCP/IP 监听器的 Oracle9i 数据库或更高版本

    • 随 Microsoft Visual Studio .NET(包括 Microsoft .NET 框架 1.0 或更高版本)安装的 C# / VB.NET

    创建数据库对象

    此方法文档使用DeptTab表和Test_Arraybind数据库存储过程。使用SQL*Plus以任意用户身份连接到数据库,然后运行如下命令来创建数据库对象:

    DROP TABLE depttab;  CREATE TABLE depttab (deptno NUMBER(2), dname VARCHAR2(14));CREATE OR REPLACE PROCEDURE Test_Arraybind(pdeptno NUMBER, pdname VARCHAR2) IS  BEGIN    INSERT INTO depttab (deptno, dname) VALUES ( pdeptno, pdname);    COMMIT;  END;

    代码预演

    包括所需命名空间:.cs.vb文件中的“general declarations”部分中添加对命名空间的引用非常值得,这样可避免以后在脚本中限定其使用:

    C#
    using System;using System.Data;using Oracle.DataAccess.Client;
    Visual Basic .NET
    Imports SystemImports System.DataImports Oracle.DataAccess.Client

    1. 使用 ODP.NET 建立到 Oracle 数据库的连接:

    C#
    // STEP 1// NOTE:Substitute User ID, Password, Data Source // as per your database setupstring connectStr = "User Id=scott; Password=tiger; Data Source=orcl9i";// Initialize connectionOracleConnection connection;connection = new OracleConnection(connectStr);connection.Open();
    Visual Basic .NET
    ' STEP 1' NOTE:Substitute User ID, Password, Data Source' as per your database setupDim connectStr As String = "User Id=Scott; Password=tiger; Data Source=orcl9i"' Initialize connectionDim connection As OracleConnectionconnection = New OracleConnection(connectStr)connection.Open()

    2. 初始化OracleCommand对象:

    C#
    // STEP 2// Set command to execute Test_Arraybind database stored procedureOracleCommand cmd1 = new OracleCommand("",connection);cmd1.CommandText= "Test_Arraybind";;cmd1.CommandType = CommandType.StoredProcedure;
    Visual Basic .NET
    'STEP 2' Set command to execute Test_Arraybind database stored procedureDim cmd1 As OracleCommand = New OracleCommand("", connection)cmd1.CommandText = "Test_Arraybind"cmd1.CommandType = CommandType.StoredProcedure

    3. 用 Deptno 和 Dname 的多组值初始化数组。ArrayBindCount属性确定命令执行次数及作为OracleCommand的一部分绑定的数组元素的个数:

    C#
    // STEP 3// Initialize array with dataint[] myArrayDeptNo = new int[3]{1, 2, 3};String[] myArrayDeptName = {"Dev", "QA", "Facility"};// Set the ArrayCount for command to 3 i.e. max. number of rows in the// preceding arrays.cmd1.ArrayBindCount = 3;
    Visual Basic .NET
    ' STEP 3' Initialize array with dataDim myArrayDeptNo As Int16() = {1, 2, 3}Dim myArrayDeptName As String() = {"Dev", "QA";, "Facility"}' Set the ArrayCount for command to 3 i.e. max.' number of rows in the' preceding arrayscmd1.ArrayBindCount = 3

    4. 将 Oracle 参数deptNoParam 和 deptNameParam的值设置为所创建的数组:

    C#
    // STEP 4// Instantiate Oracle parameter corresponding to DeptNoOracleParameter deptNoParam = new OracleParameter("deptno",OracleDbType.Int32);deptNoParam.Direction = ParameterDirection.Input;// Bind Array containing Department numbers "deptNoParam" Oracle ParameterdeptNoParam.Value = myArrayDeptNo;// Add Oracle Parameter to Command cmd1.Parameters.Add(deptNoParam);// Similarly bind Dept Name parameterOracleParameter deptNameParam = new OracleParameter("deptname",OracleDbType.Varchar2);deptNameParam.Direction = ParameterDirection.Input;deptNameParam.Value = myArrayDeptName;cmd1.Parameters.Add(deptNameParam);
    Visual Basic .NET
    ' STEP 4' Instantiate Oracle parameter corresponding to DeptNoDim deptNoParam As OracleParameter = New OracleParameter("deptno", OracleDbType.Int32)deptNoParam.Direction = ParameterDirection.Input' Bind Array containing Department numbers "deptNoParam" Oracle ParameterdeptNoParam.Value = myArrayDeptNo ' Add Oracle Parameter to Command cmd1.Parameters.Add(deptNoParam)' Similarly bind Dept Name parameterDim deptNameParam As OracleParameter = New OracleParameter("deptname";,OracleDbType.Varchar2)deptNameParam.Direction = ParameterDirection.InputdeptNameParam.Value = myArrayDeptNamecmd1.Parameters.Add(deptNameParam)

    5. 一旦执行调用存储过程的命令,则在一个数据库回程中多次调用该数据库存储过程:

    C#
    // STEP 5// Execute the command calling stored proceduretry{cmd1.ExecuteNonQuery();Console.WriteLine("{0} Rows Inserted" , cmd1.ArrayBindCount);}catch (Exception e){Console.WriteLine("Execution Failed:"  e.Message);}
    Visual Basic .NET
    ' STEP 5' Execute the command calling stored procedureTrycmd1.ExecuteNonQuery()Console.WriteLine("{0} Rows Inserted", cmd1.ArrayBindCount)Catch e As ExceptionConsole.WriteLine("Execution Failed:"  e.Message)End Try

    6. 从应用程序退出之前,先清除DeptTab表:

    C#
    // Step 6// Cleanup DeptTab table dataOracleCommand cmd2 = new OracleCommand("",connection);// Delete all the rows from the DeptTab tablecmd2.CommandText = "DELETE depttab WHERE deptno = :1";// Bind with an array of 3 itemscmd2.ArrayBindCount = 3;OracleParameter param1 = new OracleParameter();param1.OracleDbType = OracleDbType.Int32;param1.Value = myArrayDeptNo;cmd2.Parameters.Add(param1);// Execute the delete statement through commandtry{cmd2.ExecuteNonQuery(); Console.WriteLine("Cleaned DeptTab table data");}catch (Exception e){Console.WriteLine("Cleanup Failed:{0}" ,e.Message);}finally{// Dispose the OracleCommand objectscmd1.Dispose();cmd2.Dispose();// Close and Dispose the OracleConnection objectconnection.Close();connection.Dispose();}
    Visual Basic .NET
    ' Step 6' Cleanup DeptTab table dataDim cmd2 As OracleCommand = New OracleCommand("", connection)' Delete all the rows from the DeptTab tablecmd2.CommandText = "DELETE depttab WHERE deptno = :1" ' Bind with an array of 3 itemscmd2.ArrayBindCount = 3Dim param1 As OracleParameter = New OracleParameter()param1.OracleDbType = OracleDbType.Int32param1.Value = myArrayDeptNocmd2.Parameters.Add(param1)' Execute the delete statement through commandTrycmd2.ExecuteNonQuery()Console.WriteLine("Cleaned DeptTab table data";)Catch e As ExceptionConsole.WriteLine("Cleanup Failed:{0}", e.Message)Finally' Dispose the OracleCommand objectscmd1.Dispose()cmd2.Dispose()' Close and Dispose the OracleConnection objectconnection.Close()connection.Dispose()End Try

    设置并运行此方法文档程序

    1. 打开 Visual Studio.NET。

    2. 创建控制台应用程序项目:

    C# 用 C# 创建一个控制台应用程序项目。默认情况下,将Class1.cs 添加到项目中。Visual Basic .NET 用 Visual Basic .NET 创建控制台应用程序项目。默认情况下,将Module1.vb添加到项目中。

    3. 请确保您的项目包含对System、Oracle.DataAccessSystem.Data命名空间的引用。如果这些引用不存在,则添加对这些命名空间的引用。

    4. 复制代码:

    C#

    使用 Solution Explorer 打开Class1.cs。有关为此方法文章用 C# 编写的代码的完整清单,请单击这里。复制此代码,覆盖Class1.cs的内容。保存此文件。

    Visual Basic .NET

    使用 Solution Explorer 打开Module1.vb。有关为此方法文章用 VB.NET 编写的代码的完整清单,请单击这里。复制此代码,覆盖Module1.vb的内容。保存此文件。

    5. 按照代码的步骤 1中的数据库设置修改用户 Id、口令及数据源。

    6. 要编译并运行此应用程序,请按下Ctrl F5。这将如图 1.1所示显示输出:


    图 1.1 – 输出的屏幕截图

  • 相关阅读:
    CodingSouls团队-个人博客(八)
    CodingSouls团队-个人博客(七 )
    vuedraggable
    idea创建springboot(脚手架创建)
    线上环境解决nginx访问laravel除了根目录全是404的问题
    前端使用工具网站
    阿里云内容协作平台(ccp)的基本使用
    photoshop CS6修改启动界面
    laravel 增强代码提示功能插件(barryvdh / laravel-ide-helper)
    laravel代码调试工具(laravel/telescope)
  • 原文地址:https://www.cnblogs.com/softwareking/p/2017100.html
Copyright © 2011-2022 走看看