如何发送空值如果datetime输入值为null [英] How to send null value If datetime input value is null

查看:62
本文介绍了如何发送空值如果datetime输入值为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 of Null, the code would work; otherwise, you'll get a missing parameter error.

In order to pass Null as the parameter value, you have to use DBNull.Value. However, if your ternary expression has return values of DBNull and DateTime, the compiler won't be able to determine the return type of the expression.

The trick is to force the return type to be object, by explicitly boxing the DateTime value. (The overhead of boxing the value is irrelevant, since it's being passed to a method parameter of type object, 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆