如何从LINQ设置Sql DateTime为null [英] How to set Sql DateTime to null from LINQ
问题描述
当我们插入一个新的客户。
相应代码:
客户=新客户();
customer.CustomerName = txt_name.Text;
customer.DOB = dtp_dob.Checked? DateTime.Parse(dtp_dob.Value.ToString(yyyy-MM-dd)):// send null here;
customer.DOB是System.DateTime。
我试过的是:
1)
code> customer.DOB = dtp_dob.Checked? DateTime.Parse(dtp_dob.Value.ToString(yyyy-MM-dd)):SqlDateTime.Null;
但这不会成功,因为SqlDateTime无法转换为System.DateTime。
2)
DateTime? nullDateTime = new DateTime();
customer.DOB = dtp_dob.Checked? DateTime.Parse(dtp_dob.Value.ToString(yyyy-MM-dd)):nullDateTime.Value
在这种情况下,构建成功,但会抛出SqlDateTime溢出异常。
那么如何传递空值
LINQ数据类中DOB成员的属性
其中许多建议将Auto Generated Value设置为true,而不是插入任何值。的确,它适用于插入情况。
但现在假设已经有一个客户条目,其中一些datetime值设置为DOB字段。现在用户想要清除此值(删除birthdate),那么在这种情况下,我必须使用UpdateOn传递一个空值,以清除相应客户行中提交的datetime。
非常感谢您提前:)
您应该可以使用:
客户=新客户();
customer.CustomerName = txt_name.Text;
customer.DOB = dtp_dob.Checked
? DateTime.Parse(dtp_dob.Value.ToString(yyyy-MM-dd))
:(DateTime?)null;
请注意 null
到 DateTime?
。当前的问题是编译器无法根据操作数解决条件表达式的类型。
您第二次尝试的问题是您正在创建一个新的不可为空的DateTime值,即0001年1月1日。您可以将其更改为:
DateTime? nullDateTime = null;
customer.DOB = dtp_dob.Checked
? DateTime.Parse(dtp_dob.Value.ToString(yyyy-MM-dd))
:nullDateTime;
I have two cases when I would need to set DateTime field in Sql to null. I use C# and LINQ to SQL Classes. I read many questions on stackoverflow which are like my question but still I feed mine a bit different.
When we insert a new customer.
Corresponding code:
Customer customer = new Customer();
customer.CustomerName = txt_name.Text;
customer.DOB = dtp_dob.Checked ? DateTime.Parse(dtp_dob.Value.ToString("yyyy-MM-dd")) : //send null here;
customer.DOB is System.DateTime.
What I tried is:
1)
customer.DOB = dtp_dob.Checked ? DateTime.Parse(dtp_dob.Value.ToString("yyyy-MM-dd")) : SqlDateTime.Null;
But this will not succeed as SqlDateTime cannot be converted to System.DateTime.
2)
DateTime? nullDateTime = new DateTime();
customer.DOB = dtp_dob.Checked ? DateTime.Parse(dtp_dob.Value.ToString("yyyy-MM-dd")) : nullDateTime.Value
In this case, build succeeds but it will throw an SqlDateTime overflow exception.
So then how to pass null value
Property of DOB Member in LINQ Dataclass
Many of them suggest to set Auto Generated Value to true and not to insert any value. True, it works in insert cases.
But now assume, there is already a customer entry where some datetime value is set to DOB field. Now user wants to clear this value (remove the birthdate), then in this case I have to pass a null value using UpdateOn to clear the datetime filed in corresponding customer row.
Thank you very much in advance :)
You should be able to use:
Customer customer = new Customer();
customer.CustomerName = txt_name.Text;
customer.DOB = dtp_dob.Checked
? DateTime.Parse(dtp_dob.Value.ToString("yyyy-MM-dd"))
: (DateTime?) null;
Note the cast of null
to DateTime?
. The current problem is that the compiler can't work out the type of the conditional expression based on the operands.
The problem with your second attempt is that you're creating a new non-nullable DateTime value, i.e. 1st January 0001 AD. You could change that to:
DateTime? nullDateTime = null;
customer.DOB = dtp_dob.Checked
? DateTime.Parse(dtp_dob.Value.ToString("yyyy-MM-dd"))
: nullDateTime;
这篇关于如何从LINQ设置Sql DateTime为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!