3

ExecuteScalar()有时返回空对象 - 不为空 - 尽管记录存在。当我用 quickwatch 分析这个对象时,我看到它object.GetType()等于DbNull. 我可以处理这个空对象,但我需要知道为什么它有时会返回空对象,尽管记录存在。

string sql = @"SELECT SentDate 
               FROM dbo.EmailOut                                    
               WHERE ID = @ID";
SqlCommand cmd = new SqlCommand(sql, _cnn);
cmd.CommandType = System.Data.CommandType.Text;
cmd.Parameters.Add(new SqlParameter("@ID", ID));
object obj = cmd.ExecuteScalar();
if (obj == null)
    return false
sentDate = (DateTime)obj;
cmd.Dispose();

大多数情况下,我的查询运行良好。你能检查我的代码吗?

4

1 回答 1

9

A return value of null means that no record was found.

A return value of DBNull means that a record was found, but the value of SentDate in that record is NULL.

于 2013-11-08T08:42:40.663 回答