您的位置:首页 > 数据库

C# Retrieving Associative Arrays from PL/SQL Code

2010-08-08 13:43 447 查看
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());
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: