我使用DataReader读取我的数据库。
有些行没有fdate值。
因此,当我将空日期转换为DateTime时会出错。
我该如何检查字段是否为空?
AdsCommand cmd = conn.CreateCommand();
cmd.CommandText = "select name,fdate from abc";
AdsDataReader reader = cmd.ExecuteReader();
DateTime flsdate = (reader["fdate"].Equals(null))? Convert.ToDateTime(reader["fdate"]) : DateTime.Today;
我尝试使用“Equals”,但它不起作用。
有人知道如何检查空对象以避免转换错误吗?
谢谢!
==
或!=
而不是.Equals
。此外,您可能需要检查DBNull
,而不仅仅是null
。(是的,它们是不同的)。 - Servy