C# 处理 OracleCommand 参数中的空值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1166056/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 09:48:33  来源:igfitidea点击:

Handling null values in OracleCommand parameters

c#odp.net

提问by Dan U.

I'm trying to determine how to specify null as a parameter value in an OracleCommand using the following C# code. I've excerpted the relevant bits, but basically the point is if sal_id comes in with the value 0 it should be stored as null. I've tried Null, "Null", and a couple other things but so far no luck.

我正在尝试使用以下 C# 代码确定如何在 OracleCommand 中将 null 指定为参数值。我摘录了相关位,但基本上重点是如果sal_id 的值为0,则应将其存储为空值。我已经尝试过 Null、“Null”和其他一些东西,但到目前为止还没有运气。

cmd.CommandText = "INSERT INTO tcustomer(cust_id, salutation_id) VALUES(ORADBA.SEQCUST.NEXTVAL, :salid) RETURNING cust_id INTO :newcid" ;

if (sal_id==0) {
  cmd.Parameters.Add("salid", Null) ;
} else {
  cmd.Parameters.Add("salid", sal_id) ;
}

cmd.Parameters.Add("newcid", OracleDbType.Int32).Direction = ParameterDirection.ReturnValue ;

cmd.ExecuteNonQuery() ;

String newcidval = cmd.Parameters["newcid"].Value.ToString() ;
cmd.Dispose() ;

采纳答案by Andrew Hare

Try System.DBNullinsead of null.

尝试System.DBNull代替null.

The DBNull class represents a nonexistent value. In a database, for example, a column in a row of a table might not contain any data whatsoever. That is, the column is considered to not exist at all instead of merely not having a value. A DBNull object represents the nonexistent column

DBNull 类表示不存在的值。例如,在数据库中,表行中的列可能不包含任何数据。也就是说,该列被认为根本不存在,而不仅仅是没有值。DBNull 对象表示不存在的列

回答by ParPar

For me DBNull.Valuedidn't work, so I did it like this:

对我来说DBNull.Value没有用,所以我这样做了:

cmd.Parameters.Add("salid", "Null") ;

and it worked great.

效果很好。

回答by Kamran Qadir

DBNull can not be assigned to any other data type so have to implicit convert it to object.

DBNull 不能分配给任何其他数据类型,因此必须将其隐式转换为对象。

cmd.Parameters.Add(new OracleParameter(":number_column", OracleType.Number)).Value = (s.Class.HasValue) ? s.Class.Value : (object)DBNull.Value;

where s.Class is int? and parameter value is set as (object)DBNull.Valuein case it is null

哪里 s.Class 是 int?并且参数值设置为(object)DBNull.Value如果它为空

回答by bonh

You could add an empty parameter (it will be null to start), and only set the value if sal_id != 0:

您可以添加一个空参数(开始时将为空),并且仅在以下情况下设置该值sal_id != 0

var p = cmd.Parameters.Add("salid", OracleDbType.Int32);
if (sal_id != 0) {
    p.Value = sal_id;
}

回答by Thor

DBNull.Valueworks for some but I needed to cast it to a Oracle Type that implemented INullable. I recommend this approach if you are using a Nullable data type:

DBNull.Value适用于某些人,但我需要将其强制转换为实现INullable. 如果您使用的是 Nullable 数据类型,我推荐这种方法:

    (Oracle.ManagedDataAccess.Types.OracleDecimal)command.Parameters["pKey"].Value)).IsNull

回答by drumsk

you can check Status of your OracleParameter object - if it equals to OracleParameterStatus.NullFetched, nothing was fetched, otherwise use param's Value attribute.

您可以检查您的 OracleParameter 对象的状态 - 如果它等于 OracleParameterStatus.NullFetched,则未获取任何内容,否则使用 param 的 Value 属性。

Example:

例子:

var _myParam = new OracleParameter( "p_param", OracleDbType.Object, ParameterDirection.Output ) { UdtTypeName = "my_schema.MY_TYPE" };

... (add command parameter)
... (execute command)

var _myResponse = _myParam.Status == OracleParameterStatus.NullFetched ? null : MapMyTypeFromOracleType( (MyType)_myParam.Value );