简体   繁体   English

在C#和Oracle之间转换对象类型

[英]Cast object type between C# and Oracle

I have got an issue with cast object. 我有一个关于转换对象的问题。 I tried to resolve it for a couple of hours. 我尝试解决了几个小时。 It has to do with data type - raw in Oracle and byte in C#. 它与数据类型有关-在Oracle中是原始数据,在C#中是字节。 They seem not compatible. 它们似乎不兼容。

Function in Oracle 在Oracle中的功能

function dupCheck(i_vendor varchar2,i_transactionnumber varchar2) return raw
is
transactionId raw(16);

BEGIN

    select id into transactionId from (select tx.id,row_number() over (order by tx.trans_time asc) as seqnum
    from test_tx_log tx
    where tx.transactionnumber = i_transactionnumber and lower(tx.vendor) = lower(i_vendor)) tx where seqnum = 1;

    return transactionId;

    exception
    when no_data_found then
    return transactionId;
END;

C# C#

using (DbCommand command = connection.CreateCommand())
                {
                    command.CommandType = CommandType.StoredProcedure;
                    command.CommandText = "mca_test_package.dupCheck";
                    command.AddParameter("i_vendor", DbType.String, tx.Vendor);
                    command.AddParameter("i_transactionnumber", DbType.String, tx.TransactionNumber.Trim());
                    command.AddParameter("transactionId", DbType.Byte, DBNull.Value, ParameterDirection.ReturnValue,16);

                    command.ExecuteNonQuery();
                    var txId = new Guid((byte[])command.Parameters["transactionId"].Value);



                    byte[] buffer = new byte[16];
                    Guid id = new Guid(buffer);
                    bool result = (id == txId);

                    if (result)
                    {
                        tx.status = "Success";
                        Console.WriteLine("No Duplicate {0}", tx);

                    }
                    else
                    {
                        Console.WriteLine("Duplicate {0}", tx);
                        tx.status = "RejectedDuplicate";
                    }

Get the error 得到错误

在此处输入图片说明

Note : My assumption is transactionid is an output parameter. 注意 :我的假设是transactionid是一个输出参数。

transactionId is null .so you need to handle the null value transactionId为null。因此您需要处理null值

if(!Convert.IsDBNull(command.Parameters["transactionId"].Value))
{

    var txId = new Guid((byte[])command.Parameters["transactionId"].Value);
}

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

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