2013-03-08 48 views
0

如果文本框有值,是否可以将文本框值转换为日期时间,否则使用以下方式在数据库中保存为NUll。这是VS 2005使用C#将它保存到SQL Server 2005.我知道,如果文本框有日期,然后调用此函数,我可以在手之前检查。我有一个函数可以将其他值保存到数据库中,即使日期没有输入到文本框中。这只是我想给这里的一个例子如果文本框有日期转换为日期时间其他NULL

new BusinessLogic.BizLogic().InsertDate(CID, Convert.ToDateTime(txtDate.Text)); 

在此先感谢您。

回答

3
DateTime value; 

new BusinessLogic.BizLogic().InsertDate(CID, 
DateTime.TryParse(txtDate.Text, out value) ? value : (DateTime?)null); 
0

是的。但是您需要在该语句之外提取转换逻辑。

DateTime dt; 

if (!DateTime.TryParse(TextBox.text, dt)) 
     dt = null; 
相关问题