如何发送空值如果datetime输入值为null [英] How to send null value If datetime input value is null
问题描述
cmd.Parameters.AddWithValue(@ review_date,Convert.ToDateTime(review_date));
在这种情况下,review_date是null将发生异常
此查询下方不起作用
cmd.Parameters.AddWithValue( @review_date,string.IsNullOrEmpty(review_date)?(DateTime?)null或DateTime.MinValue或DBNULL.Value:Convert.ToDateTime(review_date));
DBNULL .Value:System.datetime和system.DBNull之间没有转换异常发生
DateTime.MinValue:堆栈溢出异常发生
但是在此之下类型使用查询其工作原理
string mindate =01/01/1990;
cmd.Parameters.AddWithValue(@ review_date ,string.IsNullOrEmpty(review_date)?Convert.ToDateTime(mindate):Convert.ToDateTime(review_date));
(DateTime? )null
选项关闭 - 代码将编译,但参数呃不会传递给查询。如果参数的默认值为Null
,则代码可以正常工作;否则,你会得到一个缺失的参数错误。
为了传递Null
作为参数值,你必须使用DBNull.Value
。但是,如果您的三元表达式的返回值为DBNull
和DateTime
,则编译器将无法确定返回表达式的类型。
诀窍是强制返回类型为object
,通过明确装箱DateTime
值。 (装箱值的开销是无关紧要的,因为它被传递给object
类型的方法参数,所以无论如何它都需要装箱。)
这应该有效:
cmd.Parameters.AddWithValue( @ review_date, string .IsNullOrEmpty(review_date)?DBNull。值:( object )Convert.ToDateTime(review_date));
检查以下条件.. ......希望这有帮助
if(review_date!= null)
{
cmd.Parameters.AddWithValue(@ review_date ,Convert.ToDateTime(review_date));
}
else
{
//无论你想要什么发送
}
这应该这样做:
cmd.Parameters.AddWithValue(@ review_date ,
String.IsNullOrEmpty(review_date)?DBNull.Value:Convert。 ToDateTime(review_date));
cmd.Parameters.AddWithValue("@review_date",Convert.ToDateTime(review_date));
In this case review_date is null Exception will be occurs
Below This Query not working
cmd.Parameters.AddWithValue("@review_date", string.IsNullOrEmpty(review_date) ? (DateTime?)null or DateTime.MinValue or DBNULL.Value: Convert.ToDateTime(review_date));
DBNULL.Value:no conversion between System.datetime and system.DBNull Exception occurs
DateTime.MinValue: stack overflow Exception occurs
But Below This type use Query its working
string mindate="01/01/1990";
cmd.Parameters.AddWithValue("@review_date", string.IsNullOrEmpty(review_date) ? Convert.ToDateTime(mindate) : Convert.ToDateTime(review_date));
The(DateTime?)null
option is close - the code will compile, but the parameter won't be passed to the query. If the parameter had a default value ofNull
, the code would work; otherwise, you'll get a missing parameter error.
In order to passNull
as the parameter value, you have to useDBNull.Value
. However, if your ternary expression has return values ofDBNull
andDateTime
, the compiler won't be able to determine the return type of the expression.
The trick is to force the return type to beobject
, by explicitly boxing theDateTime
value. (The overhead of boxing the value is irrelevant, since it's being passed to a method parameter of typeobject
, so it would need to be boxed anyway.)
This should work:
cmd.Parameters.AddWithValue("@review_date", string.IsNullOrEmpty(review_date) ? DBNull.Value : (object)Convert.ToDateTime(review_date));
check following condition........Hope this helps
if(review_date!=null)
{
cmd.Parameters.AddWithValue("@review_date",Convert.ToDateTime(review_date));
}
else
{
//Whatever you want to send
}
This should do it:
cmd.Parameters.AddWithValue("@review_date", String.IsNullOrEmpty(review_date) ? DBNull.Value : Convert.ToDateTime(review_date));
这篇关于如何发送空值如果datetime输入值为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!