• C# Retrieving Associative Arrays from PL/SQL Code


    CREATE OR REPLACE PACKAGE ProductsPackage IS
      TYPE DecimalArray IS TABLE OF DECIMAL INDEX BY BINARY_INTEGER;
      TYPE StringArray IS TABLE OF VARCHAR2(255) INDEX BY BINARY_INTEGER;
      PROCEDURE proc_GetAllProductNames(ProdNames OUT StringArray);
    END ProductsPackage;
     /
    CREATE OR REPLACE PACKAGE BODY ProductsPackage IS
      PROCEDURE proc_GetAllProductNames(ProdNames OUT StringArray)
     IS
     BEGIN
        SELECT Name BULK COLLECT INTO ProdNames FROM Products;
     END;
    END ProductsPackage;

    /

    private void btnGetAllProductNames(object sender, EventArgs e)
    {
      String _connstring = "Data Source=localhost/NEWDB;User 
      Id=EDZEHOO;Password=PASS123;";
            try
            {
              OracleConnection _connObj = new OracleConnection(_connstring);
                    _connObj.Open();
                    OracleCommand _cmdObj = _connObj.CreateCommand();
                    _cmdObj.CommandText = "ProductsPackage.proc_GetAllProductNames";
                    _cmdObj.CommandType = CommandType.StoredProcedure;
     
        //Create an output parameter
                    OracleParameter _NameParam = new OracleParameter();
                    _NameParam.ParameterName = "ProdNames";
                    _NameParam.OracleDbType = OracleDbType.Varchar2 ;
                    _NameParam.Direction = ParameterDirection.Output;
                    _NameParam.CollectionType = OracleCollectionType.PLSQLAssociativeArray; 
     //You must explicitly define the number of elements to return           
     _NameParam.Size = 10;
    //Because you are retrieving an object with a variable size, you need to
    //define the size of the string returned. This size must be specified for
    //each element in the output result
     int[] intArray= new int[10];
     int _counter;
     for (_counter = 0; _counter < 10; _counter++) {intArray[_counter] = 255;}
     _NameParam.ArrayBindSize = intArray;
    //Execute the stored procedure
     _cmdObj.Parameters.Add(_NameParam);
     _cmdObj.ExecuteNonQuery();
    //For VARCHAR2 data types, an array of OracleString objects is returned
     String _result="";
     OracleString[] stringArray = (OracleString[])_NameParam.Value;
     for (_counter = 0; _counter <= stringArray.GetUpperBound(0); _counter++)
     {
         OracleString _outputString = stringArray[_counter];
         _result = _result + _outputString.Value + "\n";
     }
     MessageBox.Show("Product names are:\n" + _result);
     
     _connObj.Close();
     _connObj.Dispose();
     _connObj = null; }
      catch (Exception ex)
     {
      MessageBox.Show(ex.ToString());
     }
    }

  • 相关阅读:
    Python+Selenium 自动化实现实例Xpath捕捉元素的几种方法
    Python+Selenium 自动化实现实例获取页面元素信息(百度首页)
    Example4_6(用类名调用类方法)
    Example5_2(子类对象的构造方法)
    关键字super
    字符数组中的大小写字母变换/使用对象数组
    参数传值(Example4_7.Example4_8,Example4_9)
    私有变量和私有方法(Example4_15和Example4_16)
    Example4_11(关键字this)
    成员变量(对象共享类变量及常量的用法)Example4_4//Example4_5
  • 原文地址:https://www.cnblogs.com/kingwangzhen/p/1795116.html
Copyright © 2020-2023  润新知