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());
}
}
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());
}
}
相关文章推荐
- C# Passing Associative Arrays to PL/SQL Code
- PL/SQL Associative Arrays
- [导入]How can I run another application or batch file from my Visual C# .NET code?
- pl/sql developer快速输入select * from
- create a C# context menu from code
- Invoking Webservice from PL/SQL (UTL_DBWS&UTL_HTTP)
- HOWTO:Turn Off PDA Display while your application is running.(VB.Net/c#+.Net CF+PPC2003) from CodeProject
- Passing an ARRAY from Java to PL/SQL
- Send Emails from PL/SQL .
- How to run an existing SQL Server Agent Job from C#
- c#里用pl/sql匿名块和绑定变量在执行insert后获取当前序列值
- Performing SQL Operations from PL/SQL
- invoke Javascript from C# code
- Enums and Structs in C#(C#里的枚举和结构) (from codeproject)
- pl/sql快速输入select * from等语句
- Beginning C# Objects:From Concepts to Code
- Import data from EXCEL to Oracle table with PL/SQL Developer
- How to make a callback to C# from C/C++ code
- PL/SQL如何快速打出select * from 记住登陆密码
- 数组作为参数传给存储过程(from:http://www.codeproject.com/KB/database/SQLArrayParameterHandling.aspx)