Ado.Net 调用Oracle的存储过程

Win2003+Vs2003+Oracle9.0
建立Oracle的存储过程,如果有参数,不论是in或者out类型的,都不能够指定参数的大小,理由很简单,无法判断。
建立过程:
Create or replace procedure procedure_test(inpara1   in varchar2,
 inpara2   
in varchar2
)
as
begin
 ..
end procedure_test;
/
我们无法预知procedure_test的调用者传参数时,inpara1,inpara2的长度会是多少。

如果使用Ado.Net ,用OleDbCommand调用这个过程,那么必须指定对应inpara1,inpara2的OleDbParameter的长度。

即使参数类型是out 的,也必须指定长度,曾经看O'Reilly的Ado.Net in a Nullshell一书,在讲调用out类型参数的过程时,所举的例子的参数为Int 型,没有再显示指定参数长度Size,当时误以为对于out类型的无须指定,但是在实际应用中,对于字符型参数,如果没有长度,会报0Size的错误,必须显示指定Size,才可以调用过程;而对于Int类型的,都有自己的默认长度吧。

附调用参数为out类型的函数:
/// <summary>
        
/// 调用存储过程,参数都是out型的,
        
/// 返回参数的长度不定义
        
/// </summary>
        
/// <param name="procedureName">过程名</param>
        
/// <param name="para">参数,接收返回值</param>

        [LastModified("2006-01-05","调用存储过程,过程的参数都是out类型的")]
        [LastModified(
"2006-01-17","设置参数的Size")]
        
public virtual void CallProcedureOut(string procedureName,ref string[] para)
        
{
            
try
            
{
                ConnectionPrepare(
true);
                OleDbCommand Cmd
=OraCon.CreateCommand();
                Cmd.CommandType
=CommandType.StoredProcedure;
                Cmd.CommandText
=procedureName;
                
//假设接收的参数的长度最大为800
                for(int i=0;i<para.Length;i++)
                
{
                    OleDbParameter Parameter
=new OleDbParameter(@"para"+i,OleDbType.VarChar,800);
                    Parameter.Direction
=ParameterDirection.Output;
                    Cmd.Parameters.Add(Parameter);
                }

                                
                
//调用存储过程
                Cmd.ExecuteNonQuery();
                
//获取调用存储过程后参数的值
                for(int i=0;i<para.Length;i++)
                
{
                    para[i]
=Cmd.Parameters[i].Value.ToString().Trim().Replace("\0","");
                }

            }

            
catch(OleDbException oraex)
            
{
                
throw oraex;
            }

            
catch(Exception ex)
            
{
                
throw ex;
            }

            
finally
            
{
                ConnectionPrepare(
false);
            }

        }

附O'Reilly的例子:
过程:
CREATE Procedure CustomerAdd
(
@FullName nvarchar(50),
@Email nvarchar(50),
@Password nvarchar(50),
@CustomerID int OUTPUT
)
AS
INSERT INTO Customers
(
FullName,
EMailAddress,
Password
)
VALUES
(
@FullName,
@Email,
@Password
)
SELECT
@CustomerID = @@Identity
GO
Ado.Net 调用:
using System;
using System.Data;
using System.Data.SqlClient;
public class AddCustomer
{
public static void Main()
{
string connectionString = "Data Source=localhost;" +
"Initial Catalog=store;Integrated Security=SSPI";
string procedure = "CustomerAdd";
// Create ADO.NET objects.
SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd 
= new SqlCommand(procedure, con);
// Configure command and add input parameters.
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter param;
param 
= cmd.Parameters.Add("@FullName", SqlDbType.NVarChar, 50);
param.Value 
= "John Smith";
param 
= cmd.Parameters.Add("@Email", SqlDbType.NVarChar, 50);
param.Value 
= "john@mydomain.com";
param 
= cmd.Parameters.Add("@Password", SqlDbType.NVarChar, 50);
param.Value 
= "opensesame";
// Add the output parameter.
param = cmd.Parameters.Add("@CustomerID", SqlDbType.Int);
param.Direction 
=
 ParameterDirection.Output;
// Execute the command.
con.Open();
cmd.ExecuteNonQuery();
con.Close();
Console.WriteLine(
"New customer has ID of " + param.Value);
}

}

posted on 2006-01-17 15:18  Pierce  阅读(1319)  评论(0编辑  收藏  举报

导航