简体   繁体   English

c#Dapper中的dbms_output.Put_line

[英]dbms_output.Put_line in c# Dapper

I need return a variable oracle to C#, however I use Dapper, so exist a way to receive a output by dapper? 我需要将一个变量oracle返回给C#,但是我使用Dapper,所以存在一种通过dapper接收输出的方法吗?

I tried like this 我试过这样的

ORACLE    ...
            ,NULL                                                               /*CLIENTE_GRUPO_CPF*/
            ,'P'                                                                /*LOTE_PRODUCAO_PEDIDO*/
            ,'N'                                                                /*LIBERACAO_COMERCIAL*/
            ,NULL                                                               /*USUARIO_LIB_COMERCIAL*/
            ,NULL                                                               /*DATA_LIB_COMERCIAL*/
            ,NULL                                                               /*HORA_LIB_COMERCIAL*/
            ,'L'                                                                );
    COMMIT;
    dbms_output.put_line(var_zi_controle_id);
    END ;


C#  
var id = conn.QueryFirstOrDefault<int>(sql);

but this way the answer is 0. 但这样的答案是0。

Using dbms_output.put_line(var_zi_controle_id); 使用dbms_output.put_line(var_zi_controle_id); to Output a variable is incorrect, this would not work with programming languages / database driver like ODP.Net, this is for console printing and verification. 输出变量是不正确的,这不适用于像ODP.Net这样的编程语言/数据库驱动程序,这适用于控制台打印和验证。

Dapper consist of set of extension methods which extends IDbConnection interface of ADO.Net, internally for Oracle you would fill in ODP.Net - OracleConnection object, now when you check the ODP.Net guide for calling any of the PL SQL Stored procedures, you have following options: Dapper包含一组扩展方法,它们扩展了ADO.Net的IDbConnection接口,在Oracle内部你会填写ODP.Net - OracleConnection对象,现在当你检查ODP.Net指南调用任何PL SQL存储过程时,你有以下选择:

  1. Bind a Output or Return parameter and fill the parameter in the PL SQL procedure. 绑定Output或Return参数并填充PL SQL过程中的参数。 Using Dapper, you need DynamicParameter for binding parameters beside Input like Output / Return 使用Dapper,你需要DynamicParameter来绑定输入,如输出/返回
  2. Other option is way you are currently trying, you are expecting Select statement to return the value as integer, so what you need is Select var_zi_controle_id in the procedure, which will come as a QueryResult, but still you would need a Type T, which contains an integer value, here you would get a Type / class with one integer property var_zi_controle_id . 其他选项是你当前正在尝试的方式,你期望Select语句将值返回为整数,所以你需要的是在过程中Select var_zi_controle_id ,它将作为QueryResult,但你仍然需要一个Type T,它包含一个整数值,这里你将得到一个带有一个整数属性var_zi_controle_id的Type / class。 Query<T> though returns IEnumerable<T> Query<T>虽然返回IEnumerable<T>

My Preferred way would be adding the Output parameter, assign in the Stored Procedureand retrieve value, check the links underneath for good reference. 我的首选方法是添加输出参数,在存储过程中分配并检索值,检查下面的链接以获得良好的参考。 A simple example would be: 一个简单的例子是:

 var dynamicParameters = new DynamicParameters();
 dynamicParameters.Add("var_zi_controle_id",0,DbType.Int32,ParameterDirection.Output)

Parameter Direction options InputOutput and ReturnValue can also be used, but ReturnValue needs explicit return value from the procedure Parameter Direction选项也可以使用InputOutputReturnValue ,但ReturnValue需要来自过程的显式返回值

// Execute the Stored Procedure //执行存储过程

conn.Execute(@"ProcedureName", dynamicParameters, commandType: CommandType.StoredProcedure);

// Fetch O/p Parameter Value from dynamicParameters //从dynamicParameters获取O / p参数值

var id = dynamicParameters.Get<int>("var_zi_controle_id");

Its important that Output parameter var_zi_controle_id , which bind to the stored procedure is set, before the call returns, for value to be fetched 重要的是在调用返回之前设置绑定到存储过程的输出参数var_zi_controle_id ,以获取要获取的值

OracleCommand SQL Parameters Binding OracleCommand SQL参数绑定

https://docs.oracle.com/database/121/ODPNT/OracleCommandClass.htm#ODPNT458 https://docs.oracle.com/database/121/ODPNT/OracleCommandClass.htm#ODPNT458

https://docs.oracle.com/cd/E11882_01/win.112/e23174/OracleParameterClass.htm#ODPNT1722 https://docs.oracle.com/cd/E11882_01/win.112/e23174/OracleParameterClass.htm#ODPNT1722

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM