[英]How to check an output DbParameter for null value?
I'm using System.Data.Common.DbCommand.ExecuteNonQueryAsync
to call an Oracle stored procedure that has one output parameter o_err_msg
which (you guessed it) is a VARCHAR2 error message when an exception occurs or null when the procedure succeeds.我正在使用
System.Data.Common.DbCommand.ExecuteNonQueryAsync
调用具有一个输出参数o_err_msg
的 Oracle 存储过程,该参数(您猜对了)是发生异常时的 VARCHAR2 错误消息,或过程成功时为 null。 So, after calling `ExecuteNonQueryAsync, I'm trying to check if that parameter is null, to see if there was an error or not.因此,在调用 `ExecuteNonQueryAsync 后,我试图检查该参数是否为空,以查看是否有错误。 But can't find a way to actually do it.
但找不到真正做到这一点的方法。
I've tried comparing (through both the ==
operator and the Equals
method) the Value
property of the parameter to null, to DBNull.Value
and calling System.Convert.IsDBNull
on it.我已经尝试将参数的
Value
属性(通过==
运算符和Equals
方法)与 null、 DBNull.Value
和对其调用System.Convert.IsDBNull
进行比较。 All of these return null.所有这些都返回空值。 I've even tried converting it to string (either through the
ToString
method or System.Convert.ToString
) and then calling string.IsNullOrEmpty
, but the resulting string is "null"
.我什至尝试将其转换为字符串(通过
ToString
方法或System.Convert.ToString
),然后调用string.IsNullOrEmpty
,但结果字符串为"null"
。
The code I'm running is somewhat similar to this:我正在运行的代码与此有些类似:
DbCommand cmd = dbConnection.CreateCommand();
cmd.CommandText = "PCKG_FOO.PROC_BAR";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new OracleParameter("o_err_msg", OracleDbType.Varchar2, 4000, "", ParameterDirection.Output));
await cmd.ExecuteNonQueryAsync();
if (cmd.Parameters["o_err_msg"].Value != null)
throw new InvalidOperationException(cmd.Parameters["o_err_msg"].Value.ToString());
Even though, in this example, I'm initializing the parameters as OracleParameter
, in my code I only have DbParameter
accessible and I'd prefer if I didn't have to cast it to OracleParameter
or anything Oracle-specific.尽管在本示例中,我将参数初始化为
OracleParameter
,但在我的代码中,我只有DbParameter
可访问,如果我不必将其OracleParameter
为OracleParameter
或任何特定于 Oracle 的内容,我更愿意这样做。
Is there any way I can do this?有什么办法可以做到这一点吗?
Thanks in advance.提前致谢。
I encountered the same problem today, the fowling code should be a workarround.我今天遇到了同样的问题,fowling 代码应该是一种解决方法。
OracleParameter out1 = new OracleParameter("out1", OracleDbType.Varchar2, ParameterDirection.Output);
out1.Size = 1000;
Oracle.ManagedDataAccess.Types.OracleString out1Value = (Oracle.ManagedDataAccess.Types.OracleString)out1.Value;
if (!out1Value.IsNull)
{
// do something
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.