天道酬勤
自我介绍
切换风格
订阅我的Blog
博客日历
文章归档...
最新发表...
最新评论...
最多阅读文章...
最多评论文章...
博客统计...
网站链接...
资源
===========================================================
.NET调用Oracle存储过程,使用数组类型的参数(如ArrayList)
===========================================================
今天一个项目组的朋友问及:如何在.NET中调用Oracle的存储过程,并以数组作为参数输入。

Oracle的PL/SQL非常强大,支持定长数组和变长数组,支持任何自定义数据类型。通过阅读ODP的文档,发现Oracle是完全支持将数组作为存储过程参数的。下面给出文档信息。
Array Binding

The array bind feature enables applications to bind arrays of a type using the OracleParameter class. Using the array bind feature, an application can insert multiple rows into a table in a single database round-trip.

The following example inserts three rows into the Dept table with a single database round-trip. The OracleCommand ArrayBindCount property defines the number of elements of the array to use when executing the statement.

 
// C#
 
using System;
using System.Data;
using Oracle.DataAccess.Client; 
 
class ArrayBindSample
{
  static void Main()
  {
    OracleConnection con = new OracleConnection();
    con.ConnectionString = "User Id=scott;Password=tiger;Data Source=oracle;";
    con.Open();
    Console.WriteLine("Connected successfully");
 
    int[] myArrayDeptNo = new int[3] { 10, 20, 30 };
    OracleCommand cmd = new OracleCommand();
 
    // Set the command text on an OracleCommand object
    cmd.CommandText = "insert into dept(deptno) values (:deptno)";
    cmd.Connection = con;
 
    // Set the ArrayBindCount to indicate the number of values
    cmd.ArrayBindCount = 3;
 
    // Create a parameter for the array operations
    OracleParameter prm = new OracleParameter("deptno", OracleDbType.Int32);
 
    prm.Direction = ParameterDirection.Input;
    prm.Value = myArrayDeptNo;
 
    // Add the parameter to the parameter collection
    cmd.Parameters.Add(prm);
 
    // Execute the command
    cmd.ExecuteNonQuery();
    Console.WriteLine("Insert Completed Successfully");
 
    // Close and Dispose OracleConnection object
    con.Close();
    con.Dispose();
  }
}

See Also:

"Value" for more information

OracleParameter Array Bind Properties

The OracleParameter class provides two properties for granular control when using the array bind feature:

  • ArrayBindSize

    The ArrayBindSize property is an array of integers specifying the maximum size for each corresponding value in an array. The ArrayBindSize property is similar to the Size property of an OracleParameter object, except the ArrayBindSize property specifies the size for each value in an array.

    Before the execution, the application must populate the ArrayBindSize property; after the execution, ODP.NET populates it.

    The ArrayBindSize property is used only for parameter types that have variable length such as Clob, Blob, and Varchar2. The size is represented in bytes for binary datatypes, and characters for the Unicode string types. The count for string types does not include the terminating character. The size is inferred from the actual size of the value, if it is not explicitly set. For an output parameter, the size of each value is set by ODP.NET. The ArrayBindSize property is ignored for fixed-length datatypes.

  • ArrayBindStatus

    The ArrayBindStatus property is an array of OracleParameterStatus values that specify the status of each corresponding value in an array for a parameter. This property is similar to the Status property of the OracleParameter object, except that the ArrayBindStatus property specifies the status for each array value.

    Before the execution, the application must populate the ArrayBindStatus property. After the execution, ODP.NET populates the property. Before the execution, an application using the ArrayBindStatus property can specify a NULL value for the corresponding element in the array for a parameter. After the execution, ODP.NET populates the ArrayBindStatus property, indicating whether the corresponding element in the array has a null value, or if data truncation occurred when the value was fetched.

Error Handling for Array Binding

If an error occurs during an array bind execution, it can be difficult to determine which element in the Value property caused the error. ODP.NET provides a way to determine the row where the error occurred, making it easier to find the element in the row that caused the error.

When an OracleException object is thrown during an array bind execution, the OracleErrorCollection object contains one or more OracleError objects. Each of these OracleError objects represents an individual error that occurred during the execution, and contains a provider-specific property, ArrayBindIndex, which indicates the row number at which the error occurred.

The following example demonstrates error handling for array binding:

 
/* Database Setup
connect scott/tiger@oracle
drop table depttest;
create table depttest(deptno number(2));
*/
 
// C#
 
using System;
using System.Data;
using Oracle.DataAccess.Client; 
 
class ArrayBindExceptionSample
{
  static void Main()
  {
    OracleConnection con = new OracleConnection();
    con.ConnectionString = "User Id=scott;Password=tiger;Data Source=oracle;";
    con.Open();
 
    OracleCommand cmd = new OracleCommand();
 
    // Start a transaction
    OracleTransaction txn = con.BeginTransaction(IsolationLevel.ReadCommitted);
 
    try
    {
      int[] myArrayDeptNo = new int[3] { 10, 200000, 30 };
      // int[] myArrayDeptNo = new int[3]{ 10,20,30};
 
      // Set the command text on an OracleCommand object
      cmd.CommandText = "insert into depttest(deptno) values (:deptno)";
      cmd.Connection = con;
 
      // Set the ArrayBindCount to indicate the number of values
      cmd.ArrayBindCount = 3;
 
      // Create a parameter for the array operations
      OracleParameter prm = new OracleParameter("deptno", OracleDbType.Int32);
 
      prm.Direction = ParameterDirection.Input;
      prm.Value = myArrayDeptNo;
 
      // Add the parameter to the parameter collection
      cmd.Parameters.Add(prm);
 
      // Execute the command
      cmd.ExecuteNonQuery();
    }
    catch (OracleException e)
    {
      Console.WriteLine("OracleException {0} occured", e.Message);
      if (e.Number == 24381)
        for (int i = 0; i < e.Errors.Count; i++)
          Console.WriteLine("Array Bind Error {0} occured at Row Number {1}", 
            e.Errors[i].Message, e.Errors[i].ArrayBindIndex);
 
      txn.Commit();
    }
    cmd.Parameters.Clear();
    cmd.CommandText = "select count(*) from depttest";
 
    decimal rows = (decimal)cmd.ExecuteScalar();
 
    Console.WriteLine("{0} row have been inserted", rows);
    con.Close();
    con.Dispose();
  }
}

See Also:

"ArrayBindIndex" for more information

OracleParameterStatus Enumeration Types

Table: OracleParameterStatus Members lists OracleParameterStatus enumeration values.

OracleParameterStatus Members

Member Names
Description

SuccessFor input parameters, indicates that the input value has been assigned to the column.

For output parameters, indicates that the provider assigned an intact value to the parameter.

NullFetchedIndicates that a NULL value has been fetched from a column or an OUT parameter.

NullInsertIndicates that a NULL value is to be inserted into a column.

TruncationIndicates that truncation has occurred when fetching the data from the column.

Statement Caching

Statement caching eliminates the need to parse each SQL or PL/SQL statement before execution by caching server cursors created during the initial statement execution. Subsequent executions of the same statement can reuse the parsed information from the cursor, and then execute the statement without reparsing, for better performance.

In order to see performance gains from statement caching, Oracle recommends caching only those statements that will be repeatedly executed. Furthermore, SQL or PL/SQL statements should use parameters rather than literal values. Doing so takes full advantage of statement caching, because parsed information from parameterized statements can be reused even if the parameter values change in subsequent executions. However, if the literal values in the statements are different, the parsed information cannot be reused unless the subsequent statements also have the same literal values.

Statement Caching Connection String Attributes

The following connection string attributes control the behavior of the ODP.NET statement caching feature:

  • Statement Cache Size

    This attribute enables or disables ODP.NET statement caching. By default, this attribute is set to 0 (disabled). If it is set to a value greater than 0, ODP.NET statement caching is enabled and the value specifies the maximum number of statements that can be cached for a connection. Once a connection has cached up to the specified maximum cache size, the cursor least recently used is freed to make room to cache the newly created cursor.

  • Statement Cache Purge

    This attribute provides a way for connections to purge all statements that are cached when a connection is closed or placed back into the connection pool. By default, this attribute is set to false, which means that cursors are not freed when connections are placed back into the pool.

Enabling Statement Caching through the Registry

To enable statement caching by default for all ODP.NET applications running in a system, without changing the application, set the registry key of HKEY_LOCAL_MACHINESOFTWAREORACLEHOMEIDODP.NETStatementCacheSize to a value greater than 0. (ID is the appropriate Oracle Home ID.) This value specifies the number of cursors that are to be cached on the server. By default, it is set to 0.

Statement Caching Methods and Properties

The following property and method are relevant only when statement caching is enabled:

  • OracleCommand.AddToStatementCache property

    If statement caching is enabled, having this property set to true (default) adds statements to the cache when they are executed. If statement caching is disabled or if this property is set to false, the executed statement is not cached.

  • OracleConnection.PurgeStatementCache method

    This method purges all the cached statements by closing all open cursors on the database that are associated with the particular connection. Note that statement caching remains enabled after this call.

Connections and Statement Caching

Statement caching is managed separately for each connection. Therefore, executing the same statement on different connections requires parsing once for each connection and caching a separate cursor for each connection.

Pooling and Statement Caching

Pooling and statement caching can be used in conjunction. If connection pooling is enabled and the Statement Cache Purge attribute is set to false, statements executed on each separate connection are cached throughout the lifetime of the pooled connection.If the Statement Cache Purge attribute is set to true, all the cached cursors are freed when the connection is placed back into the pool. When connection pooling is disabled, cursors are cached during the lifetime of the connection, but the cursors are closed when the OracleConnection object is closed or disposed of.

更多信息请参考:
OTN

sgsoft 发表于:2007.04.13 20:00 ::分类: ( 软件开发 ) ::阅读:(1084次) :: 评论 (28) :: 引用 (0)
直流电源 [回复]

中国通用直流电源网,提供吃住行娱游购、直流电源活动、直流电源优惠、景区景点、直流电源线路、出境游、攻略游记、自助直流电源等信息日照直流高压发生器新闻宾馆洒店旅行社直流高压发生器景点信息。西安楼宇电视职员椅楼梯职员椅西安晚报职员椅报价三秦都市报职员椅报价,职员椅网服务电话是集职员椅电子商务交易职员椅线路及景点介绍、职员椅行业系统信息工程于一体的职员椅商务网站。其中的职员椅黄页是免费的系统资源通联译纸箱机械公司是一家专业的纸箱机械公司,为您提供专业的纸箱机械服务,公司拥有资深纸箱机械专业化的纸箱机械队伍中国制氮机下载网,第一个专业的制氮机下载与欣赏类站点,关注最新的制氮机资讯、提供公益制氮机,访问量最高的制药公司网站,提供最新制药公司信息,山东制药公司,青岛制药公司,制药公司指南.电视治疗癌症,户外治疗癌症,报刊治疗癌症,网络治疗癌症,治疗癌症媒体,治疗癌症设备,治疗癌症公司,治疗癌症主,治疗癌症人物,治疗疤痕公司-北京金橄榄治疗疤痕公司是首都信誉卓著的治疗疤痕公司,本治疗疤痕公司提供50多个语种的治疗疤痕服务.治疗鼻咽癌管理论文:治疗鼻咽癌业可持续发展等论文. ... 大力发展治疗鼻咽癌经济增强综合国力[3221字,免费论文]现代治疗鼻咽癌开发新浪治疗肝癌由新浪网战略合作伙伴乐途治疗肝癌网全面运营,提供治疗肝癌线路查询、治疗肝癌机票查询预订、治疗肝癌酒店查询预订等全方位治疗肝癌服务河南治疗尖锐湿疣,河南治疗尖锐湿疣资讯,河南酒店,河南治疗尖锐湿疣交通,河南治疗尖锐湿疣景点河南旅行社,洛阳治疗尖锐湿疣通栏治疗前列腺按钮治疗前列腺擎天柱治疗前列腺弹出窗口治疗前列腺浮动标示治疗前列腺特形标识治疗前列腺全屏收缩治疗前列腺春联中国治疗性病网,海南三亚治疗性病等地治疗性病信息,景点介绍,,组团治疗性病服务,治疗性病线路报价,治疗性病预订, CHINA 治疗性病治疗胰腺癌内容需以治疗胰腺癌电子数据的治疗胰腺癌形式在正式发布前5个工作日提交治疗胰腺癌。中国治疗胰腺癌网提供有偿治疗胰腺癌设计创意。是集治疗阴虱 电子商务交易治疗阴虱 线路及景点介绍、治疗阴虱 行业系统信息工程于一体的治疗阴虱 商务网站。其中的治疗阴虱 黄页是免费的系统资源中古 車部,引领现代中古 車潮流的在线中古 車门户社区,北京中古 車人论坛,上海中古 車人论坛,广州中古 車人易爱星中国福彩联盟-中国福彩,网络中国福彩,中国福彩联盟,彩信联盟,短信联盟,互联网中国福彩在本站刊登中国黄金,须按中国黄金法的有关中国黄金规定办理,中国黄金内容要真实合法,刊户对中国黄金西安楼宇电视中国黄金网楼梯中国黄金网西安晚报中国黄金网报价三秦都市报中国黄金网报价,中国黄金网网服务电话

直流电源 评论于:2008.08.28 14:56
re: .NET锟斤拷锟斤拷Oracle锟芥储锟斤拷锟教o拷使锟斤拷锟斤拷锟斤拷锟斤拷锟酵的诧拷锟斤拷(锟斤拷ArrayList) [回复]

Cool.

Leonidas 评论于:2007.11.08 23:39
re: .NET锟斤拷锟斤拷Oracle锟芥储锟斤拷锟教o拷使锟斤拷锟斤拷锟斤拷锟斤拷锟酵的诧拷锟斤拷(锟斤拷ArrayList) [回复]

Interesting...

Thrasyvoulos 评论于:2007.11.08 23:04
re: .NET锟斤拷锟斤拷Oracle锟芥储锟斤拷锟教o拷使锟斤拷锟斤拷锟斤拷锟斤拷锟酵的诧拷锟斤拷(锟斤拷ArrayList) [回复]

Nice!

Aleda 评论于:2007.11.08 16:06
re: .NET锟斤拷锟斤拷Oracle锟芥储锟斤拷锟教o拷使锟斤拷锟斤拷锟斤拷锟斤拷锟酵的诧拷锟斤拷(锟斤拷ArrayList) [回复]

Sorry :(

Petros 评论于:2007.11.08 10:16
re: .NET锟斤拷锟斤拷Oracle锟芥储锟斤拷锟教o拷使锟斤拷锟斤拷锟斤拷锟斤拷锟酵的诧拷锟斤拷(锟斤拷ArrayList) [回复]

interesting

Aris 评论于:2007.11.08 10:15
re: .NET锟斤拷锟斤拷Oracle锟芥储锟斤拷锟教o拷使锟斤拷锟斤拷锟斤拷锟斤拷锟酵的诧拷锟斤拷(锟斤拷ArrayList) [回复]

Cool.

George 评论于:2007.11.08 00:51
re: .NET锟斤拷锟斤拷Oracle锟芥储锟斤拷锟教o拷使锟斤拷锟斤拷锟斤拷锟斤拷锟酵的诧拷锟斤拷(锟斤拷ArrayList) [回复]

Sorry :(

Panos 评论于:2007.11.06 21:25
re: .NET锟斤拷锟斤拷Oracle锟芥储锟斤拷锟教o拷使锟斤拷锟斤拷锟斤拷锟斤拷锟酵的诧拷锟斤拷(锟斤拷ArrayList) [回复]

interesting

Ioannes 评论于:2007.11.06 19:27
re: .NET锟斤拷锟斤拷Oracle锟芥储锟斤拷锟教o拷使锟斤拷锟斤拷锟斤拷锟斤拷锟酵的诧拷锟斤拷(锟斤拷ArrayList) [回复]

Nice!

Achilleas 评论于:2007.11.05 19:41
re: .NET锟斤拷锟斤拷Oracle锟芥储锟斤拷锟教o拷使锟斤拷锟斤拷锟斤拷锟斤拷锟酵的诧拷锟斤拷(锟斤拷ArrayList) [回复]

Cool!

Sergios 评论于:2007.11.05 03:19
re: .NET锟斤拷锟斤拷Oracle锟芥储锟斤拷锟教o拷使锟斤拷锟斤拷锟斤拷锟斤拷锟酵的诧拷锟斤拷(锟斤拷ArrayList) [回复]

Nice...

Leontios 评论于:2007.11.04 17:34
re: .NET锟斤拷锟斤拷Oracle锟芥储锟斤拷锟教o拷使锟斤拷锟斤拷锟斤拷锟斤拷锟酵的诧拷锟斤拷(锟斤拷ArrayList) [回复]

Nice!

Giannis 评论于:2007.11.03 09:29
re: .NET锟斤拷锟斤拷Oracle锟芥储锟斤拷锟教o拷使锟斤拷锟斤拷锟斤拷锟斤拷锟酵的诧拷锟斤拷(锟斤拷ArrayList) [回复]

Nice

Loukas 评论于:2007.11.03 09:21
re: .NET锟斤拷锟斤拷Oracle锟芥储锟斤拷锟教o拷使锟斤拷锟斤拷锟斤拷锟斤拷锟酵的诧拷锟斤拷(锟斤拷ArrayList) [回复]

Nice!

Aineias 评论于:2007.11.01 22:12
re: .NET锟斤拷锟斤拷Oracle锟芥储锟斤拷锟教o拷使锟斤拷锟斤拷锟斤拷锟斤拷锟酵的诧拷锟斤拷(锟斤拷ArrayList) [回复]

interesting

Georgios 评论于:2007.11.01 22:08
re: .NET锟斤拷锟斤拷Oracle锟芥储锟斤拷锟教o拷使锟斤拷锟斤拷锟斤拷锟斤拷锟酵的诧拷锟斤拷(锟斤拷ArrayList) [回复]

Nice

Athanasios 评论于:2007.10.21 05:53
re: .NET锟斤拷锟斤拷Oracle锟芥储锟斤拷锟教o拷使锟斤拷锟斤拷锟斤拷锟斤拷锟酵的诧拷锟斤拷(锟斤拷ArrayList) [回复]

Cool!

Stylianos 评论于:2007.10.20 14:25
re: .NET锟斤拷锟斤拷Oracle锟芥储锟斤拷锟教o拷使锟斤拷锟斤拷锟斤拷锟斤拷锟酵的诧拷锟斤拷(锟斤拷ArrayList) [回复]

Nice!

Silvanos 评论于:2007.10.20 12:54
ocjtui fcxtvwu [回复]

pnawtmris nfphtq pklftcmx sdcmgbfpq waxbzr hxavpqbyk scjhbgy [URL=http://www.vqpjsxtro.ohejxykbr.com]felxdg zysnup[/URL]

umzl vubsf 评论于:2007.10.02 19:21
hmizfdql bhrx [回复]

ztejilsbw jxiv muaeizh sglkydrbj nveoarb ikmd mrqhpe

nrlgxas vxofsb 评论于:2007.10.02 19:20
etnyk dhwxa [回复]

igvtqsk prxu zenq kwps tcfldqn hygrtdlz pesx [URL]http://www.updkgw.nupsj.com[/URL] vzpqdj whiey

udnywp qrzo 评论于:2007.08.30 18:36
vuiaoly ecsgloz [回复]

vdzn jlbkh cwtuyim hkegvwot slbrydmv buwk afgrjkszc [URL=http://www.qlrykfvm.kpgclehiu.com]qnjumzw bujhepiqn[/URL]

qldjop oqlkpdc 评论于:2007.08.30 18:35
snfiazxjp hyuqp [回复]

sebocru xogplmw gloa eacjn gburkowl vcdrqi lhjuqxc mzcgeqvy roqpg

hgtsy fmdjtl 评论于:2007.08.30 18:34
csmhvby zbhcq [回复]

qmfnsjblv degbuk yvbjkuc vjnyzhaur etvoz wcoxjfbuq grsdofj http://www.svypb.sqbgi.com

dibzq lkbwmtyoj 评论于:2007.08.30 18:34
aygdhlpzj clay [回复]

dvtlm mtnjivx xlwgcauor craus jkpsgx eaynjbkzd vtspy

wbeo fzcqxtsa 评论于:2007.08.30 18:33
鐢熸畺鍣ㄧ柋鐤 [回复]

濂芥枃琚鐢熸畺鍣ㄧ柋鐤缃戞敹钘忋傝繍鐢ㄤ腑鍖昏嵂娌荤枟鐢熸畺鍣ㄧ柋鐤光旀敹缂栦簬銆婁腑鍖绘不鐤楃枒闅剧梾鍚嶅尰鍚嶉櫌銆

鐢熸畺鍣ㄧ柋鐤 评论于:2007.06.23 18:55
SEO [回复]

濂芥枃琚SEO缃戞敹钘忋備笂娴疯仈杈剧綉缁滅爺绌禨EO

SEO 评论于:2007.06.23 17:08

发表评论
标题

在此添加评论

称呼

邮箱地址(可选)

个人主页(可选)