Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在ASP.NET中调用PLSQL过程_C#_Oracle_Visual Studio 2013_Plsql_Asp.net 4.5 - Fatal编程技术网

C# 在ASP.NET中调用PLSQL过程

C# 在ASP.NET中调用PLSQL过程,c#,oracle,visual-studio-2013,plsql,asp.net-4.5,C#,Oracle,Visual Studio 2013,Plsql,Asp.net 4.5,我有一个项目要立即开始使用VS 2013、.NET 4.5.1和Oracle 12。我需要做的是使用提供给我的API,我需要调用PLSQL中的过程并获取参数的值。根据这些值,我需要为表生成记录。有人能告诉我如何从过程中调用这些参数吗。我不确定我是否以一种可以理解的方式发布了这个问题。我首先建议您安装Oracle.ManagedDataAccess NuGet包()。它添加了驱动程序和库以启用到Oracle数据库的连接。之后,您必须在web.config或app.config中配置连接 一旦在项目

我有一个项目要立即开始使用VS 2013、.NET 4.5.1和Oracle 12。我需要做的是使用提供给我的API,我需要调用PLSQL中的过程并获取参数的值。根据这些值,我需要为表生成记录。有人能告诉我如何从过程中调用这些参数吗。我不确定我是否以一种可以理解的方式发布了这个问题。

我首先建议您安装Oracle.ManagedDataAccess NuGet包()。它添加了驱动程序和库以启用到Oracle数据库的连接。之后,您必须在web.config或app.config中配置连接

一旦在项目中安装了该数据库,连接到Oracle并执行过程与使用其他数据库的方式非常相似。例如,假设您有以下PLSQL包:

CREATE OR REPLACE 
PACKAGE MYPACKAGE AS 

  PROCEDURE MYPROCEDURE(
    p_param1    IN VARCHAR2,
    p_param2    IN NUMBER,
    p_output1   OUT VARCHAR2,
    p_output2   OUT NUMBER);

  FUNCTION MYFUNCTION(
    p_param1    IN VARCHAR2,
    p_output1   OUT NUMBER)
  RETURN VARCHAR2;

END MYPACKAGE;
执行该过程的代码如下所示:

using Oracle.ManagedDataAccess.Client;
using Oracle.ManagedDataAccess.Types;
using System;
using System.Configuration;

//I'm not including the namespace, class or function declaration, but the following should be inside your fuction
// myconnection is the your oracle connection string as defined in your config (web.config or app.config)
using (OracleConnection cnx = new OracleConnection(ConfigurationManager.ConnectionStrings["myconnection"].ConnectionString))
{
   cnx.Open();
   // You prepare the statement here
   OracleCommand commProc = new OracleCommand();
   commProc.Connection = cnx;
   commProc.CommandText = @"MYPROCEDURE.MYPROCEDURE";
   commProc.CommandType = System.Data.CommandType.StoredProcedure;

   // Here you add all the parameters (in and out) for the procedure
   commProc.Parameters.Add(new OracleParameter("p_param1", OracleDbType.Varchar2)
   {
       Value = v_param1, //This would be the C# variable or value you're putting in
       Size = 9 //This has to be the expected maximum size for a string value in your PL/SQL code.
    });

    commProc.Parameters.Add(new OracleParameter("p_param2", OracleDbType.Decimal)
   {
        Value = v_param2, //This would be the C# variable or value you're putting in
    });
   commProc.Parameters.Add(new OracleParameter("p_output1", OracleDbType.Varchar2)
   {
       Direction = System.Data.ParameterDirection.Output, //For output params, you don't specify values, but you have to specify direction.
       Size = 500 //This has to be the expected maximum size for the string value in your PL/SQL code.
    });

    commProc.Parameters.Add(new OracleParameter("p_output2", OracleDbType.Decimal)
   {
        Direction = System.Data.ParameterDirection.Output, //For output params, you don't specify values, but you have to specify direction.
    });

   // Here you actually execute the procedure.
   commProc.ExecuteNonQuery();

   // Once the procedure is exectued, you can access the values for the output params using the commProc.Parameters list.
   string v_output1 = commProc.Parameters["p_output1"]?.Value?.ToString();
   decimal v_output2 = (decimal) commProc.Parameters["p_output2"]?.Value;

   // You prepare the statement here
   OracleCommand commFunc = new OracleCommand();
   commFunc.Connection = cnx;
   commFunc.CommandText = @"MYPROCEDURE.MYFUNCTION";
   commFunc.CommandType = System.Data.CommandType.StoredProcedure;

   // Here you add all the parameters (in and out) for the procedure
   // When calling functions, the first parameter must be the return value expected from the function. Here you can name it as you wish. I usually name them return_value
   commFunc.Parameters.Add(new OracleParameter("return_value", OracleDbType.Varchar2)
   {
       Direction = System.Data.ParameterDirection.ReturnValue, //For return params, you don't specify values, but you have to specify direction.
       Size = 500 //This has to be the expected maximum size for a string value in your PL/SQL code.
    });

   commFunc.Parameters.Add(new OracleParameter("p_param1", OracleDbType.Varchar2)
   {
       Value = v_param1, //This would be the C# variable or value you're putting in
       Size = 9 //This has to be the expected maximum size for a string value in your PL/SQL code.
    });

    commFunc.Parameters.Add(new OracleParameter("p_output1", OracleDbType.Decimal)
   {
        Direction = System.Data.ParameterDirection.Output, //For output params, you don't specify values, but you have to specify direction.
    });

   // Here you actually execute the procedure.
   commFunc.ExecuteNonQuery();

   // Once the procedure is exectued, you can access the values for the output params using the commFunc.Parameters list.
   string v_return = commProc.Parameters["return_value"]?.Value?.ToString();
   decimal v_output1 = (decimal) commFunc.Parameters["p_output1"]?.Value;
}

希望这能让你知道从哪里开始。您可以在Oracle的官方网站上阅读有关使用ODP.NET连接.NET和Oracle的更多信息:

附上一些您尝试过的代码far@ArunPratap我不知道从哪里开始,而且还在学习过程中。我需要开始开发,这就是为什么我接触了你提到的API。调用存储过程的是API调用吗?或者您的代码需要调用存储过程?如果是API,那么在调用API时会遇到什么问题?您知道如何使用ADO.NET驱动程序for Oracle在Oracle中从.NET应用程序执行数据库操作吗@ChetanRanpariya我需要调用存储过程并获取参数值。我怎么能在C#中做到这一点?请给我提供任何我可以跟随的样品或文章谢谢你上传答案。请原谅我的愚蠢想法,但是我应该在哪里添加提供的代码?我没有看到你的代码中包含任何公共类没有问题。。。我给您的代码是一个片段,所有内容(除了从顶部使用导入)都将进入类的方法中,您需要在其中调用Oracle过程。我通常使用静态方法创建静态类,这些静态方法可以在我的应用程序中的任何位置调用。