Just a short post to hopefully help anyone else in the same situation where you need to pass the Oracle SysDate function as a parameter to a function or stored procedure (sproc). I'm converting old VB6 code and have been trying to use the latest and greatest .Net features, which forced me to discover this limitation. When you're adding input parameters you need to provide the value, of course, but in this case the value comes from the server. The only way I initially solved it was by creating a new function specifically for the case of SysDate (no IN Date parameter) since the original function was fine with any other date that you would pass. Then I dug into the OO4O-based code we'd been using to see how it was managing it and discovered that anonymous PL/SQL blocks were the answer. So I converted to using that so my new code can be more consistent with existing function structures we have.

I'm open to critiques and suggestions on how to improve this though. I'm using ODP.Net 12.1.1 (the managed version) though I don't think that matters much except for the OracleDbTypeEx which I think came in at 11.1. That happens to be quite important but I'll leave it to the reader to look up differences between DbType, OracleDbType and OracleDbTypeEx. I couldn't get the shorthand notation for Parameters.Add to work because of it since it uses OracleDbType.

vb.net Code:
  1. Try
  2.  
  3.             Dim sql As String = "begin :P1 := general_pkg.dateconvertfromGMT(:P2,:P3, SYSDATE); end;"
  4.  
  5.             Using cmd As New OracleCommand(sql, conn)
  6.                 Dim P1 As OracleParameter = New OracleParameter()
  7.                 P1.ParameterName = "P1"
  8.                 P1.OracleDbTypeEx = OracleDbType.Date
  9.                 P1.Direction = ParameterDirection.Output
  10.                 cmd.Parameters.Add(P1)
  11.                 Dim P2 As OracleParameter = New OracleParameter()
  12.                 P2.ParameterName = "P2"
  13.                 P2.OracleDbTypeEx = OracleDbType.Single
  14.                 P2.Direction = ParameterDirection.Input
  15.                 P2.Value = -5
  16.                 cmd.Parameters.Add(P2)
  17.                 Dim P3 As OracleParameter = New OracleParameter()
  18.                 P3.ParameterName = "P3"
  19.                 P3.OracleDbTypeEx = OracleDbType.Varchar2
  20.                 P3.Direction = ParameterDirection.Input
  21.                 P3.Value = "USA"
  22.                 cmd.Parameters.Add(P3)
  23.                 cmd.ExecuteNonQuery()
  24.                 Console.WriteLine("Date: " & P1.Value.ToString)
  25.                 Dim temp As Date = CDate(P1.Value)
  26.                 Console.WriteLine("add 20 min " & CStr(temp.AddMinutes(20)))
  27.             End Using
  28.  
  29.         Catch exOra As OracleException
  30.             errmsg = exOra.ToString
  31.  
  32.         Catch ex As SystemException
  33.             errmsg = ex.ToString
  34.  
  35.         End Try