Convert from Oracle's RAW(16) to .NET's GUID

前端 未结 4 792
挽巷
挽巷 2020-12-04 14:34

I\'m having difficulties manually debugging an .NET application where the Guid values differ from .NET to Oracle.

  • Where C# reads:
    • 17D89D326C2142
4条回答
  •  情话喂你
    2020-12-04 15:11

    Just use always your standard GUID in .NET...

    When you want to insert some GUID into Oracle you just call Guid.ToString ( "N") and feed that string to Oracle (in this example the param name is MyNETVAL):

    INSERT INTO MyTable ( MyRAWCol)
    SELECT HEXTORAW (SUBSTR (MyNETVal, 6, 2) || SUBSTR (MyNETVal, 4, 2) || SUBSTR (MyNETVal, 2, 2) || SUBSTR (MyNETVal, 0, 2) || SUBSTR (MyNETVal, 10, 2) || SUBSTR (MyNETVal, 8, 2) || SUBSTR (MyNETVal, 14, 2) || SUBSTR (MyNETVal, 12, 2) || SUBSTR (MyNETVal, 16, 16)) FROM DUAL;
    

    When you read a RAW from Oracle you use:

    SELECT 
    SUBSTR (HexV, 6, 2) || SUBSTR (HexV, 4, 2) || SUBSTR (HexV, 2, 2) || SUBSTR (HexV, 0, 2) || SUBSTR (HexV, 10, 2) || SUBSTR (HexV, 8, 2) || SUBSTR (HexV, 14, 2) || SUBSTR (HexV, 12, 2) || SUBSTR (HexV, 16, 16) AS MyNETVal
    FROM (SELECT RAWTOHEX (MyRAWCol) HexV FROM MyTable);
    

    Then you can feed the returned MyNETVal into new Guid (MyNETVal).

    This way your code always deals with the .NET format and the byte switching occurs in the Oracle-DB... you don't polute your code with conversion code and can keep the code code the same when switchig to other DBs - just change the SQL and you are up and running... the SQL could get simpler with other DBs because some of them follow the GUID format of Windows...

提交回复
热议问题