.NET调用ORACLE存储过程使用数组参数
2009-09-21 15:47
375 查看
.NET调用ORACLE存储过程使用数组参数
----------包定义
create or replace package packtest
as
type string_array is table of varchar2(21) index by binary_integer;
type int_array is table of number(4) index by binary_integer;
procedure test(v_string in string_array,
v_int out int_array);
end packtest;
--------包体
Create or replace package body packtest
as
procedure test(v_string in string_array,
v_int out int_array)
as
cursor c is select na2 from test;
v_content varchar2(21);
begin
open c;
for i in 1.. 3
loop
fetch c into v_int(i);
v_content:=v_string(i);
end loop;
close c;
end test;
end packtest;
---------程序调用
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Oracle.DataAccess;
using Oracle.DataAccess.Client;
using Oracle.DataAccess.Types;
public partial class oracle_array : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string connectionstring = string.Concat
(
"Password=gkl",
";User ID=gkl",
";Data Source=ORCL"
);
OracleConnection con = new OracleConnection(connectionstring);
try
{
con.Open();
OracleCommand cmd = con.CreateCommand();
//
//
//
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "packtest.test";
//
//定义Parameter,其中,Size是数组中的元素数量
//
OracleParameter Param1 = new
OracleParameter(@"p1", OracleDbType.Varchar2, 3);
OracleParameter Param2 = new
OracleParameter(@"p2", OracleDbType.Int32, 3);
//
Param1.Direction = ParameterDirection.Input;
Param2.Direction = ParameterDirection.Output;
// Specify that we are binding PL/SQL Associative Array
Param1.CollectionType = OracleCollectionType.PLSQLAssociativeArray;
Param2.CollectionType = OracleCollectionType.PLSQLAssociativeArray;
// Setup the values for PL/SQL Associative Array
Param1.Value = new string[3]{"First Element",
"Second Element ",
"Third Element "};
Param2.Value = null;
// Specify the maximum number of elements in the PL/SQL Associative
//Array
//
// Setup the ArrayBindSize for Param1
Param1.ArrayBindSize = new int[3] { 13, 14, 13 };
// Setup the ArrayBindStatus for Param1
Param1.ArrayBindStatus = new OracleParameterStatus[3]{
OracleParameterStatus.Success,
OracleParameterStatus.Success,
OracleParameterStatus.Success};
// Setup the ArrayBindSize for Param2
//对于Int类型的Parameter,可以不设置Size,因为本身Int定义了自身大小
//
// Param2.ArrayBindSize = new int[3]{1, 2, 1};
cmd.Parameters.Add(Param1);
cmd.Parameters.Add(Param2);
// execute the cmd
cmd.ExecuteNonQuery();
//print out the parameter's values
int[] matrixint = (int[])Param2.Value;
foreach (int t in matrixint)
{
Response.Write(t.ToString());
}
// con.Close();
}
catch (Exception ex)
{
Response.Write(string.Concat(
ex.Message,
""r"n",
ex.Source.ToString(),
""r"n",
ex.TargetSite.ToString(),
""r"n",
ex.StackTrace.ToString()
));
}
finally
{
con.Close();
}
}
}
----------包定义
create or replace package packtest
as
type string_array is table of varchar2(21) index by binary_integer;
type int_array is table of number(4) index by binary_integer;
procedure test(v_string in string_array,
v_int out int_array);
end packtest;
--------包体
Create or replace package body packtest
as
procedure test(v_string in string_array,
v_int out int_array)
as
cursor c is select na2 from test;
v_content varchar2(21);
begin
open c;
for i in 1.. 3
loop
fetch c into v_int(i);
v_content:=v_string(i);
end loop;
close c;
end test;
end packtest;
---------程序调用
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Oracle.DataAccess;
using Oracle.DataAccess.Client;
using Oracle.DataAccess.Types;
public partial class oracle_array : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string connectionstring = string.Concat
(
"Password=gkl",
";User ID=gkl",
";Data Source=ORCL"
);
OracleConnection con = new OracleConnection(connectionstring);
try
{
con.Open();
OracleCommand cmd = con.CreateCommand();
//
//
//
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "packtest.test";
//
//定义Parameter,其中,Size是数组中的元素数量
//
OracleParameter Param1 = new
OracleParameter(@"p1", OracleDbType.Varchar2, 3);
OracleParameter Param2 = new
OracleParameter(@"p2", OracleDbType.Int32, 3);
//
Param1.Direction = ParameterDirection.Input;
Param2.Direction = ParameterDirection.Output;
// Specify that we are binding PL/SQL Associative Array
Param1.CollectionType = OracleCollectionType.PLSQLAssociativeArray;
Param2.CollectionType = OracleCollectionType.PLSQLAssociativeArray;
// Setup the values for PL/SQL Associative Array
Param1.Value = new string[3]{"First Element",
"Second Element ",
"Third Element "};
Param2.Value = null;
// Specify the maximum number of elements in the PL/SQL Associative
//Array
//
// Setup the ArrayBindSize for Param1
Param1.ArrayBindSize = new int[3] { 13, 14, 13 };
// Setup the ArrayBindStatus for Param1
Param1.ArrayBindStatus = new OracleParameterStatus[3]{
OracleParameterStatus.Success,
OracleParameterStatus.Success,
OracleParameterStatus.Success};
// Setup the ArrayBindSize for Param2
//对于Int类型的Parameter,可以不设置Size,因为本身Int定义了自身大小
//
// Param2.ArrayBindSize = new int[3]{1, 2, 1};
cmd.Parameters.Add(Param1);
cmd.Parameters.Add(Param2);
// execute the cmd
cmd.ExecuteNonQuery();
//print out the parameter's values
int[] matrixint = (int[])Param2.Value;
foreach (int t in matrixint)
{
Response.Write(t.ToString());
}
// con.Close();
}
catch (Exception ex)
{
Response.Write(string.Concat(
ex.Message,
""r"n",
ex.Source.ToString(),
""r"n",
ex.TargetSite.ToString(),
""r"n",
ex.StackTrace.ToString()
));
}
finally
{
con.Close();
}
}
}
相关文章推荐
- .NET调用ORACLE存储过程使用数组参数
- .NET调用Oracle存储过程,使用数组类型的参数(如ArrayList)
- .NET调用ORACLE存储过程使用数组参数
- .NET调用Oracle存储过程,使用数组类型的参数(如ArrayList)
- .NET调用ORACLE存储过程使用数组参数
- springmvc MyBatis 调用Oracle存储过程,使用Map传递参数
- PB调用SAP的Web Service结构体数组参数方法详解(使用C#调用Web Service生成dll)
- js使用数组作为参数,直接调用push与apply方法调用push的区别
- ORACLE存储过程使用数组参数
- ORACLE存储过程使用数组参数
- java 调用Oracle存储过程(输入参数,输出参数,游标)的使用!
- 【原创】PB调用SAP的Web Service结构体数组参数方法详解(使用C#调用Web Service生成dll)
- oracle存储过程,使用数组参数
- 使用c#调用oracle存储过程获取存储过程参数信息
- Java中调用参数是数组的存储过程
- .NET用使用存储过程获取输出参数的代码示例!
- 如何使用.NET调用新浪微博接口的实例
- 如何使用数组参数
- C#中调用SQL Server存储过程主要是参数使用问题
- [转].net 调用oracle存储过程返回多个记录集