如何使用 ODP.NET 和数据集调用 Oracle 函数来插入数据? (具体例子)

发布于 2024-10-12 04:23:11 字数 1827 浏览 4 评论 0原文

我们真的对此迷失了方向,阅读 ODP.NET 2 Day+ 开发人员指南并没有帮助。我已经提供了函数定义(存储在包中),我不明白我们必须将数据集转换为什么或传递函数给什么。这是函数定义:

FUNCTION ins (
rec_data IN OUT schema.table%ROWTYPE,
p_rowid OUT ROWID,
p_execution_ts IN schema.table.update_ts%TYPE)
RETURN NUMBER

这是我们所做的(什么也不做):

// inserts data

public void insertData(DataSet Data) 
{ 
string connStr = "DATA SOURCE=someValidConnString"; 
OracleConnection conn = new OracleConnection(connStr); 
string rowID = String.Empty; 
Int32 rtnVal = 0; 
try 
{ 
conn.Open(); 
OracleCommand insCmd = new OracleCommand("PACKAGE.ins", conn); 
insCmd.CommandType = CommandType.StoredProcedure; 
OracleParameter outParam2 = new OracleParameter("retVal", OracleDbType.Varchar2,     rtnVal, 

ParameterDirection.ReturnValue); 
insCmd.Parameters.Add(outParam2); //return value 
OracleParameter inParam1 = new OracleParameter("rec_data", OracleDbType.NVarchar2,     dsACCTData.Tables

[0].Rows[0], ParameterDirection.InputOutput); 
OracleParameter outParam = new OracleParameter("p_rowid", OracleDbType.Varchar2, rowID, 

ParameterDirection.Output); 
OracleParameter inParam2 = new OracleParameter("p_execution_ts", OracleDbType.Date, 

Oracle.DataAccess.Types.OracleDate.GetSysDate(), ParameterDirection.Input); 
insCmd.Parameters.Add(inParam1);  //first in out parameter 
insCmd.Parameters.Add(outParam);  //second out parameter 
insCmd.Parameters.Add(inParam2);  //third in parameter 


insCmd.ExecuteNonQuery(); 
conn.Close(); 

} 
catch (OracleException ee) 
{ 
throw ee; 
} 
finally 
{ 
conn.Dispose(); 

} 
}

我知道这是一个非常具体的问题,但我真的迷失了。让我们假设 Oracle 函数 ins 有效(它确实有效),在这种情况下,我们根本不知道如何使用 ODP.NET 正确调用它

。非常感谢您的帮助。

编辑:这是错误消息:

ORA-06550:第 1 行,第 15 列: PLS-00306:调用“INS”时参数的数量或类型错误 ORA-06550: 第 1 行,第 7 列: PL/SQL:语句被忽略

亲切的问候, 河豚

We're really lost on this one, having read the ODP.NET 2 Day+ developer guide hasn't helped. I've provided the function definition (stored in a package), I don't understand what we have to cast the dataset to or what to pass the function. Here is the function definition:

FUNCTION ins (
rec_data IN OUT schema.table%ROWTYPE,
p_rowid OUT ROWID,
p_execution_ts IN schema.table.update_ts%TYPE)
RETURN NUMBER

Here is what we have done (which does nothing):

// inserts data

public void insertData(DataSet Data) 
{ 
string connStr = "DATA SOURCE=someValidConnString"; 
OracleConnection conn = new OracleConnection(connStr); 
string rowID = String.Empty; 
Int32 rtnVal = 0; 
try 
{ 
conn.Open(); 
OracleCommand insCmd = new OracleCommand("PACKAGE.ins", conn); 
insCmd.CommandType = CommandType.StoredProcedure; 
OracleParameter outParam2 = new OracleParameter("retVal", OracleDbType.Varchar2,     rtnVal, 

ParameterDirection.ReturnValue); 
insCmd.Parameters.Add(outParam2); //return value 
OracleParameter inParam1 = new OracleParameter("rec_data", OracleDbType.NVarchar2,     dsACCTData.Tables

[0].Rows[0], ParameterDirection.InputOutput); 
OracleParameter outParam = new OracleParameter("p_rowid", OracleDbType.Varchar2, rowID, 

ParameterDirection.Output); 
OracleParameter inParam2 = new OracleParameter("p_execution_ts", OracleDbType.Date, 

Oracle.DataAccess.Types.OracleDate.GetSysDate(), ParameterDirection.Input); 
insCmd.Parameters.Add(inParam1);  //first in out parameter 
insCmd.Parameters.Add(outParam);  //second out parameter 
insCmd.Parameters.Add(inParam2);  //third in parameter 


insCmd.ExecuteNonQuery(); 
conn.Close(); 

} 
catch (OracleException ee) 
{ 
throw ee; 
} 
finally 
{ 
conn.Dispose(); 

} 
}

I know this is a vry specific question but I'm really lost. Let's assume the Oracle function ins works (it does), in this instance we simply don't know how to call it correctly using ODP.NET

Many thanks for any help.

edit: here is the error message:

ORA-06550: line 1, column 15:
PLS-00306: wrong number or types of arguments in call to 'INS'
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored

Kind regards,
Fugu

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

第七度阳光i 2024-10-19 04:23:11

也许这可以帮助:

http://www.c-sharpcorner.com/ UploadFile/john_charles/CallingOraclestoredproceduresfromMicrosoftdotNET06222007142805PM/CallingOraclestoredproceduresfromMicrosoftdotNET.aspx

我正在寻找相同的答案,我想我找到了一些东西。第二行有输出变量的示例。

objCmd.Parameters.Add("pin_deptno", OracleType.Number).Value = 20;
objCmd.Parameters.Add("pout_count", OracleType.Number).Direction = ParameterDirection.Output;

希望该链接会有任何好处。

问候,M

maybe this could help:

http://www.c-sharpcorner.com/UploadFile/john_charles/CallingOraclestoredproceduresfromMicrosoftdotNET06222007142805PM/CallingOraclestoredproceduresfromMicrosoftdotNET.aspx

I'm looking for the same answer and I think I found something. There is example of the output variable in second row.

objCmd.Parameters.Add("pin_deptno", OracleType.Number).Value = 20;
objCmd.Parameters.Add("pout_count", OracleType.Number).Direction = ParameterDirection.Output;

Hope that link will do any good.

Regards, M

毁梦 2024-10-19 04:23:11

每件事看起来都很好。但您必须检查所有属性名称和属性类型。

在您的示例中有错误的数据类型 OracleDbType.Varchar2,因为您的函数返回 Number (在您的 Oracle 函数定义中)

OracleParameter outParam2 = new OracleParameter("retVal", OracleDbType.Varchar2, rtnVal,
参数方向.ReturnValue);

insCmd.Parameters.Add(outParam2); //返回值

Every thing looks good. But you have to check all attribute name and attribute types.

In your example have bad data type OracleDbType.Varchar2, because your function return Number (in your Oracle Function definition)

OracleParameter outParam2 = new OracleParameter("retVal", OracleDbType.Varchar2, rtnVal,
ParameterDirection.ReturnValue);

insCmd.Parameters.Add(outParam2); //return value

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文