如何从 .NET 前端在 Oracle 中使用用户定义的数据类型参数执行 Oracle 存储过程
How to execute Oracle stored procedure with User Defined DataTypes Parameters in Oracle from .NET Front-End
我使用 Oracle 作为后端,使用 C# 作为前端。
Oracle包声明如下:
CREATE OR REPLACE PACKAGE SamplePackage IS
TYPE t_number IS TABLE OF NUMBER INDEX BY PLS_INTEGER;
PROCEDURE SampleProc(run_no IN t_number, Id IN t_number);
END;
CREATE OR REPLACE PACKAGE BODY SamplePackage IS
PROCEDURE SampleProc(run_no IN t_number, Id IN t_number) IS
l_cnt NUMBER := 0;
BEGIN
FOR i IN 1 .. Id.count
LOOP
INSERT INTO SampleSchema.SampleTable
(run_no,
Id)
VALUES
(run_no(i),
Id(i));
-- commit every 1000 rows
l_cnt := l_cnt + 1;
IF (MOD(l_cnt,
1000) = 0) THEN
COMMIT;
END IF;
END LOOP;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
raise_application_error(-20001,
'SamplePackage.SampleProc: ' || SQLERRM || dbms_utility.format_error_backtrace);
END SampleProc;
END;
前端C#代码如下:
string ConnectionString = ConfigurationManager.ConnectionStrings["oracleConnection"].ConnectionString.ToString();
using (var connection = new OracleConnection(ConnectionString))
{
connection.Open();
double[] run_no = new double[2];
double[] Id = new double[2];
run_no[0] = 10;
run_no[1] = 12;
Id[0] = 100;
Id[1] = 120;
OracleParameter ora_run_no = new OracleParameter();
ora_run_no.ParameterName = "run_no";
ora_run_no.OracleDbType = OracleDbType.Double;
ora_run_no.Direction = ParameterDirection.Input;
ora_run_no.UdtTypeName = "t_number";
ora_run_no.Value = run_no;
OracleParameter ora_p_quote_id = new OracleParameter();
ora_Id.ParameterName = "Id";
ora_Id.OracleDbType = OracleDbType.Double;
ora_Id.Direction = ParameterDirection.Input;
ora_Id.UdtTypeName = "t_number";
ora_Id.Value = Id;
OracleCommand cmdInsert = new OracleCommand();
cmdInsert.Connection = connection;
cmdInsert.CommandType = CommandType.StoredProcedure;
cmdInsert.CommandText = "SampleSchema.SamplePackage.SampleProc";
cmdInsert.BindByName = true;
cmdInsert.ArrayBindCount = Id.Length;
cmdInsert.Parameters.Add(ora_run_no);
cmdInsert.Parameters.Add(ora_Id);
cmdInsert.ExecuteNonQuery();
}
这里有些混乱。当我执行 .NET 代码时,它会在 ExecuteNonQuery
处抛出错误,如下所示
ORA-06550: line 1, column 7:
PLS-00306: wrong number or types of arguments in call to 'SAMPLEPROC'
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
在 Oracle 后端,我使用 t_number
用户定义的数据类型。我唯一需要知道的是如何将其作为数组处理并从 C# 成功执行。
我对您使用的工具一无所知,但是-由于包中声明了类型,所以我会说:
ora_Id.UdtTypeName = "t_number";
应该是
ora_Id.UdtTypeName = "SampleSchema.SamplePackage.t_number";
就像您用于程序本身一样。
除此之外,由于您使用的类型是 table 个数字 ,您可以尝试使用 Oracle 的内置类型:sys.odcinumberlist
.
我使用 Oracle 作为后端,使用 C# 作为前端。
Oracle包声明如下:
CREATE OR REPLACE PACKAGE SamplePackage IS
TYPE t_number IS TABLE OF NUMBER INDEX BY PLS_INTEGER;
PROCEDURE SampleProc(run_no IN t_number, Id IN t_number);
END;
CREATE OR REPLACE PACKAGE BODY SamplePackage IS
PROCEDURE SampleProc(run_no IN t_number, Id IN t_number) IS
l_cnt NUMBER := 0;
BEGIN
FOR i IN 1 .. Id.count
LOOP
INSERT INTO SampleSchema.SampleTable
(run_no,
Id)
VALUES
(run_no(i),
Id(i));
-- commit every 1000 rows
l_cnt := l_cnt + 1;
IF (MOD(l_cnt,
1000) = 0) THEN
COMMIT;
END IF;
END LOOP;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
raise_application_error(-20001,
'SamplePackage.SampleProc: ' || SQLERRM || dbms_utility.format_error_backtrace);
END SampleProc;
END;
前端C#代码如下:
string ConnectionString = ConfigurationManager.ConnectionStrings["oracleConnection"].ConnectionString.ToString();
using (var connection = new OracleConnection(ConnectionString))
{
connection.Open();
double[] run_no = new double[2];
double[] Id = new double[2];
run_no[0] = 10;
run_no[1] = 12;
Id[0] = 100;
Id[1] = 120;
OracleParameter ora_run_no = new OracleParameter();
ora_run_no.ParameterName = "run_no";
ora_run_no.OracleDbType = OracleDbType.Double;
ora_run_no.Direction = ParameterDirection.Input;
ora_run_no.UdtTypeName = "t_number";
ora_run_no.Value = run_no;
OracleParameter ora_p_quote_id = new OracleParameter();
ora_Id.ParameterName = "Id";
ora_Id.OracleDbType = OracleDbType.Double;
ora_Id.Direction = ParameterDirection.Input;
ora_Id.UdtTypeName = "t_number";
ora_Id.Value = Id;
OracleCommand cmdInsert = new OracleCommand();
cmdInsert.Connection = connection;
cmdInsert.CommandType = CommandType.StoredProcedure;
cmdInsert.CommandText = "SampleSchema.SamplePackage.SampleProc";
cmdInsert.BindByName = true;
cmdInsert.ArrayBindCount = Id.Length;
cmdInsert.Parameters.Add(ora_run_no);
cmdInsert.Parameters.Add(ora_Id);
cmdInsert.ExecuteNonQuery();
}
这里有些混乱。当我执行 .NET 代码时,它会在 ExecuteNonQuery
处抛出错误,如下所示
ORA-06550: line 1, column 7:
PLS-00306: wrong number or types of arguments in call to 'SAMPLEPROC'ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
在 Oracle 后端,我使用 t_number
用户定义的数据类型。我唯一需要知道的是如何将其作为数组处理并从 C# 成功执行。
我对您使用的工具一无所知,但是-由于包中声明了类型,所以我会说:
ora_Id.UdtTypeName = "t_number";
应该是
ora_Id.UdtTypeName = "SampleSchema.SamplePackage.t_number";
就像您用于程序本身一样。
除此之外,由于您使用的类型是 table 个数字 ,您可以尝试使用 Oracle 的内置类型:sys.odcinumberlist
.