【问题标题】:Calling PLSQL Procedure in ASP.NET在 ASP.NET 中调用 PLSQL 过程
【发布时间】:2018-09-09 03:51:34
【问题描述】:

我有一个项目要立即使用 VS 2013、.NET 4.5.1 和 Oracle 12 启动。我需要做的是使用提供给我的 API,我需要调用 PLSQL 中的程序和获取参数的值。从这些值中,我需要为表生成记录。有人可以给我关于如何从程序中调用这些参数的任何想法。我不确定我是否以可以理解的方式发布问题。

【问题讨论】:

  • 附上你目前尝试过的一些代码
  • @ArunPratap 我不知道从哪里开始,并且仍在学习过程中。我需要开始开发,这就是我接触 SO 的原因
  • 您提到了 API。是调用存储过程的API调用吗?还是您的代码需要调用存储过程?如果是 API,您在调用 API 时遇到什么问题?你知道如何使用 Oracle 的 ADO.NET 驱动程序从 .net application_' 在 Oracle 中执行数据库操作吗? oracle.com/technetwork/topics/dotnet/index-085163.html
  • @ChetanRanpariya 我需要调用存储过程并获取参数的值。我怎么能在 C# 中做到这一点?请提供我可以关注的任何示例或文章

标签: c# oracle visual-studio-2013 plsql asp.net-4.5


【解决方案1】:

我首先建议您安装 Oracle.ManagedDataAccess NuGet 包 (https://www.nuget.org/packages/Oracle.ManagedDataAccess/)。它添加了驱动程序和库以启用与 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;
}

希望这能让您知道从哪里开始。更多关于使用ODP.NET连接.NET和Oracle的内容可以在Oracle官网阅读:http://www.oracle.com/technetwork/topics/dotnet/index-085163.html

【讨论】:

  • 感谢您上传答案。请原谅我的愚蠢问题,但我应该在哪里添加提供的代码?我没有看到您的代码中包含任何公共类
  • 没问题...我给你的​​代码是一个sn-p,一切(除了使用顶部的导入)都将进入你需要调用Oracle过程的类的方法中.我通常使用可以从我的应用程序中的任何位置调用的静态方法创建静态类。
猜你喜欢
  • 2013-04-30
  • 2015-03-09
  • 1970-01-01
  • 2016-08-24
  • 2013-05-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多